UIImagePickerController 抛出 SIGABRT

UIImagePickerController throwing SIGABRT

我正在试验 UIImagePickerController 以允许从图库中选择照片或使用相机拍照。

我按照网站 (https://makeapppie.com/2016/06/28/how-to-use-uiimagepickercontroller-for-a-camera-and-photo-library-in-swift-3-0/) 上的步骤进行操作,并在照片库中使用了它,但每当我尝试从我的应用程序调用相机时,它都会出现错误 "Thread 1: signal SIGABRT"。

这是我用来调用相机的代码:

picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)

据我了解,SIGABRT 错误预计会在模拟器中出现。然而,当我在我的 iPhone 7 上尝试它时,我希望它能工作,但它给出了同样的错误。

我已将 "Privacy - Camera Usage Description" 添加到 Info.plist 文件。

知道我做错了什么吗?

这是我 using/selecting

的完整代码
// MARK: Camera App

func openCameraApp() {
    if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
        picker.allowsEditing = false
        picker.sourceType = UIImagePickerControllerSourceType.camera
        picker.cameraCaptureMode = .photo
        picker.modalPresentationStyle = .fullScreen
        present(picker,
                animated: true,
                completion: nil)
    } else {
        noCamera()
    }
}
func noCamera(){
    let alertVC = UIAlertController(
        title: "No Camera",
        message: "Sorry, this device has no camera",
        preferredStyle: .alert)
    let okAction = UIAlertAction(
        title: "OK",
        style:.default,
        handler: nil)
    alertVC.addAction(okAction)
    present(
        alertVC,
        animated: true,
        completion: nil)
}

// MARK: Photos Albums

func showImagePicker() {
    picker.allowsEditing = false
    picker.sourceType = .photoLibrary
    //picker.modalPresentationStyle = .Popover
    present(picker,
            animated: true,
            completion: nil)
    picker.popoverPresentationController?.sourceView = self.view
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    image = chosenImage
    self.performSegue(withIdentifier: "ShowEditView", sender: self)
    dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: false, completion: nil)
}

// MARK: Seque to EditViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ShowEditView" {
        if let vc = segue.destination as? EditViewController {
            vc.image = image
            //vc.image = images[0]
        }
    }
}

忽略注释掉的两行 - 那些是我用来测试的。此代码适用于所有设备,iOS 9+,所有方向(但请记住 iPhone 始终显示为纵向),并且我在任何模拟器中都没有遇到任何问题(它没有摄像头)也不是物理设备。

有趣的是可能导致问题的一件事(不确定它是否抛出 SIGABRT)- 我正在检查后置摄像头,如果它不存在则发出警报。 (虽然没有检查前置摄像头。我什至不确定除了 iPod touch 是否没有前置摄像头。)

此外,不要忘记为 iOS 10 添加两件事到您的 info.plist:

<key>NSCameraUsageDescription</key>
<string>Used to capture new image for photo effect</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Used to select an image for photo effect</string>

您可以在描述标签中添加您想要的任何内容。如果没有这些,该应用程序将在 iOS 10 后关闭,Apple 将拒绝您的提交。 Here's link 了解更多详情。