【问题标题】:Crash after having presented UIImagePickerController to photo Library in swift 4 ; Privacy was added to info.plist在 swift 4 中将 UIImagePickerController 提交给照片库后崩溃;隐私已添加到 info.plist
【发布时间】:2020-01-11 01:53:34
【问题描述】:

使用 photoLibrary sourceType 访问 UIImagePickerController 时崩溃;隐私被添加到 info.plist

let myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = .photoLibrary
self.present(myPickerController, animated: true, completion: nil)'

错误:

无法识别的选择器发送到实例 0x28184fb40 2019-09-09 12:45:52.126598+0300 *** 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'-[__NSCFType encodeWithCoder:]: 无法识别的选择器发送到实例 0x28184fb40'

【问题讨论】:

  • 你在 Info.plist 中到底放了什么?
  • 我认为这段代码实际上没有问题。您是否点击按钮来执行该代码?可能是按钮的问题。如果您注释掉与选择器控制器相关的代码,然后点击按钮,它仍然会崩溃吗?

标签: ios swift uiimagepickercontroller


【解决方案1】:

我假设您点击一个按钮来启动您的UIImagePickerController。如果您使用界面生成器设计它,我怀疑您的按钮可能连接到多个 IBAction 方法。

转到您的故事板并右键单击该按钮。如果按钮连接到多个“已发送事件”,那么这就是您的问题。删除不相关的(也许您在代码中删除的旧的仍然连接)。

【讨论】:

    【解决方案2】:

    试试这个:

        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
    
        let cameraAction = UIAlertController(title: "Title", message: "Choose to upload a picture", preferredStyle: .alert)
        cameraAction.addAction(UIAlertAction(title: "Camera", style: .default, handler: {(action) in
    
            imagePicker.sourceType = UIImagePickerController.SourceType.camera
            self.present(imagePicker, animated: true){
    
            }
        }))
        cameraAction.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: {(action) in
    
            imagePicker.sourceType = .photoLibrary
            self.present(imagePicker, animated: true){
    
            }
        }))
        imagePicker.allowsEditing = true
        self.present(cameraAction, animated: true)
    

    【讨论】:

    • 选择源类型 .photoLibrary 时,该代码有何不同?
    • 在这里,在我的代码中,我向 UIAlertController 展示了相机和照片库的两个动作。当@Hla Khader 试图直接呈现 UIImagePickerController 时。
    • 是的,但我的意思是,如果您选择“照片库”,则会执行相同的代码。发帖人没有问如何从相机中获取图像。
    • @DavidSteppenbeckPhD 有什么问题我要提供另外两个选项的解决方案吗?此外,如果用户将来需要使用相机选项来实现,那么它会很有帮助。
    • 您的回答没有为所提出的问题提供解决方案,这就是我想说的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2020-10-05
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多