【问题标题】:Selecting "camera" causes Terminated due to signal 9选择“摄像头”导致因信号 9 而终止
【发布时间】:2017-09-19 22:23:30
【问题描述】:

在 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)
    }
}

【问题讨论】:

  • 您的 plist 文件中需要一个字符串来解释为什么您的应用需要获得使用相机的权限。有关这些字符串的详细信息,请参阅stackoverflow.com/questions/38498275/…
  • @GaryMakin 我以为我的 plist 中有这个,但我想我正在查看“图书馆”那个。这解决了问题。非常感谢!
  • @GaryMakin,您是否想将您的解决方案作为答案包含在内?

标签: ios swift memory crash uiimagepickercontroller


【解决方案1】:

您的 plist 文件中需要一个字符串来解释为什么您的应用需要获得使用相机的权限。在iOS 10 - Changes in asking permissions of Camera, microphone and Photo Library causing application to crash 的问题中有这些字符串的精彩摘要,您可以从https://stackoverflow.com/a/40734360/968577 的答案中复制一个列表

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

【讨论】:

  • 我刚刚在打开相机设置时添加了 exit(0)。它工作正常
【解决方案2】:

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

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

在你的类文件中添加这两个委托

  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)   
        }
    }

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 2018-02-25
      • 2021-08-10
      • 2012-02-23
      • 2017-07-08
      • 1970-01-01
      • 2021-10-08
      相关资源
      最近更新 更多