选择 "camera" 导致因信号 9 而终止

Selecting "camera" causes Terminated due to signal 9

在 iOS Swift 3.1 我正在尝试访问相机,就像我在其他应用程序中成功完成的一样。但是,在这个特定的应用程序中,它总是在 self.present(imagePicker, animated: true, completion: nil) 行崩溃。控制台中的消息是 Message from debugger: Terminated due to signal 9。这通常表示内存相关错误吗?

@IBAction func onChooseImageClick(_ sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum){

        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self

        //Create the AlertController and add Its action like button in Actionsheet
        let actionSheetControllerForImage: UIAlertController = UIAlertController(title: "Please select", message: "Option to select", preferredStyle: .actionSheet)

        let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
            print("Cancel")
        }
        actionSheetControllerForImage.addAction(cancelActionButton)

        let cameraActionButton: UIAlertAction = UIAlertAction(title: "Camera", style: .default)
        { action -> Void in
            print("Camera")
            if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
                imagePicker.sourceType = UIImagePickerControllerSourceType.camera
                let mediaTypes:[String] = [kUTTypeImage as String]
                imagePicker.mediaTypes = mediaTypes
                imagePicker.allowsEditing = false

                self.present(imagePicker, animated: true, completion: nil)
            } else {
                let alertController = UIAlertController(title: "error", message: "Camera not found!", preferredStyle: .alert)

                let OKAction = UIAlertAction(title: "OK", style: .cancel) { action in
                    // ...
                }
                alertController.addAction(OKAction)

                self.present(alertController, animated: true)
            }
        }
        actionSheetControllerForImage.addAction(cameraActionButton)

        let galleryActionButton: UIAlertAction = UIAlertAction(title: "Image Gallery", style: .default)
        { action -> Void in
            imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum;
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated: true, completion: nil)
        }
        actionSheetControllerForImage.popoverPresentationController?.sourceView = self.view
        actionSheetControllerForImage.addAction(galleryActionButton)
   ===> self.present(actionSheetControllerForImage, animated: true, completion: nil)
    }
}

像这样尝试我希望它会有所帮助!! 先在info.plist中加上这两点

  1. 隐私 - 相机使用说明
  2. 隐私 - 照片库使用说明

将这两位代表添加到您的 class 文件中

  1. UIImagePickerControllerDelegate
  2. UINavigationControllerDelegate

     @IBAction func onclickImageAction(_ sender: Any){
    
    print("onclickImageAction method called here")
    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.isEditing = false
    
    let actionSheet =  UIAlertController(title: "Select Profile Photo", message: "", preferredStyle: UIAlertControllerStyle.actionSheet)
    
    let libButton = UIAlertAction(title: "Select photo from library", style: UIAlertActionStyle.default){ (libSelected) in
        print("library selected")
    
    
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        self.present(imagePicker, animated: true, completion: nil)
    }
    actionSheet.addAction(libButton)
    let cameraButton = UIAlertAction(title: "Take a picture", style: UIAlertActionStyle.default) { (camSelected) in
    
        if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera))
        {
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera
            self.present(imagePicker, animated: true, completion: nil)
        }
        else
        {
            actionSheet.dismiss(animated: false, completion: nil)
            let alert = UIAlertController(title: "Error", message: "There is no camera available", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: { (alertAction) in
    
                alert.dismiss(animated: true, completion: nil)
            }))
    
        }
    
    }
    actionSheet.addAction(cameraButton)
    let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (cancelSelected) in
    
        print("cancel selected")
    
    }
    actionSheet.addAction(cancelButton)
    let albumButton = UIAlertAction(title: "Saved Album", style: UIAlertActionStyle.default) { (albumSelected) in
    
        print("Album selected")
    
        imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum
        self.present(imagePicker, animated: true, completion: nil)
    
    }
    actionSheet.addAction(albumButton)
    self.present(actionSheet, animated: true, completion:nil)
    }
    

实现这些委托方法

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{if let PickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
        {
            yourimageview.image = PickedImage
            dismiss(animated: true, completion: nil)   
        }
    }

您的 plist 文件中需要一个字符串来解释为什么您的应用需要使用相机的权限。在 , with a list you can copy from in the answer at

的问题中对这些字符串进行了很好的总结

如果没有所需的字符串,您的应用程序将以这种方式崩溃。但是,控制台输出将解释问题所在。

我刚刚在打开相机设置时添加了 exit(0)。一切正常