【问题标题】:UIImagePickerController throwing SIGABRTUIImagePickerController 抛出 SIGABRT
【发布时间】:2017-01-15 16:11:56
【问题描述】:

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

我按照网站 (https://makeapppie.com/2016/06/28/how-to-use-uiimagepickercontroller-for-a-camera-and-photo-library-in-swift-3-0/) 上的步骤操作,并为照片库提供了此功能,但每当我尝试从我的应用程序调用相机时,都会出现错误“线程 1:信号 SIGABRT”。

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

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

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

我已在 Info.plist 文件中添加了“隐私 - 相机使用说明”。

任何想法我做错了什么?

【问题讨论】:

    标签: ios iphone swift camera sigabrt


    【解决方案1】:

    这是我使用/选择的完整代码

    // 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 更多详情的链接。

    【讨论】:

    • 感谢您的回答。抱歉,我最初的问题有点模棱两可。我预计会在模拟器中出现错误,但不会在我的 iPhone 上出现。我在两者上都遇到了同样的错误。
    • 我将使用完整代码编辑我的答案。如果这没有帮助,请告诉我,我会删除它。谢谢。
    • @AlanSpark - 您的问题应该通过更新您的info.plist 文件来解决,就像@dfd 建议的那样。如果您需要更简单的方法来复制和粘贴键,然后在将新行插入info.plist 文件时手动在值列中输入值。例如,创建一个新行并将NSCameraUsageDescription 粘贴到“Key”列中。 Xcode 会自动将其更新为“隐私 - 相机访问”之类的内容,然后只需在“值”列中输入描述
    • 我想我刚才描述的内容实际上并不“更容易”,但对于那些不习惯手动编辑 info.plist 文件源的人来说更方便
    • 感谢两位的帮助。我怀疑我在做一些愚蠢的事情,结果确实如此。我做的一切都是正确的,但问题是我以某种方式连接了一个不存在的幻影 IBAction。导致坠机的是这个,而不是相机,原来是红鲱鱼。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多