UIImagePickerController关闭崩溃

时间:2020-02-13 23:26:59

标签: xcode uiimagepickercontroller

在Xcode 11.3中实现标准相机功能时遇到麻烦。当我单击“使用照片”按钮时,应用程序崩溃。单击“重新获取”按钮可以正常工作。

@IBAction func onPhotoButton(_ sender: Any) {

    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    imagePickerController.sourceType = UIImagePickerController.SourceType.camera
    present(imagePickerController, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
{
    imagePickerController.dismiss(animated: true) // crashes on this line!
    imageView.image = info[.originalImage] as? UIImage
}

崩溃发生在.dismiss行上。 错误是“致命错误:意外地发现nil,而隐式展开了一个可选值:”

我查看了以前关于类似问题的帖子,似乎没有一个可以解决此问题。

1 个答案:

答案 0 :(得分:0)

在将违规行更改为

时有效
 picker.dismiss(animated: true)