选择"相机"由于信号9导致终止

时间:2017-04-23 05:24:35

标签: ios swift memory crash uiimagepickercontroller

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

3 个答案:

答案 0 :(得分:2)

您的plist文件中需要一个字符串,用于说明您的应用需要使用相机的权限。在iOS 10 - Changes in asking permissions of Camera, microphone and Photo Library causing application to crash的问题中对这些字符串进行了很好的总结,您可以在https://stackoverflow.com/a/40734360/968577的答案中复制一个列表

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

答案 1 :(得分:0)

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

  1. 隐私权 - 相机使用说明
  2. 隐私 - 照片库使用说明
  3. 在类文件中添加这两个代理

    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)
      }
      
    3. 实施这些委托方法

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

答案 2 :(得分:0)

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