3

In iOS Swift 3.1 I'm trying to access the camera, the same way I have done successfully in other apps. However, in this one particular app, it always crashes on the self.present(imagePicker, animated: true, completion: nil) line. The message in the console is Message from debugger: Terminated due to signal 9. Does this usually signal a memory related error?

@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)
    }
}
Lastmboy
  • 1,849
  • 3
  • 21
  • 39
  • You need a string in your plist file that explains why your app need permission to use the camera. See http://stackoverflow.com/questions/38498275/ios-10-changes-in-asking-permissions-of-camera-microphone-and-photo-library-c for details about these strings. – Gary Makin Apr 23 '17 at 05:27
  • @GaryMakin I thought I had that in my plist, but I guess I was looking at the "Library" one instead. That fixed the problem. Many thanks! – Lastmboy Apr 23 '17 at 05:32
  • @GaryMakin, did you want to include your solution as an answer? – Lastmboy Apr 24 '17 at 16:28

3 Answers3

3

You need a string in your plist file that explains why your app need permission to use the camera. There's a great summary of these strings in the question at iOS 10 - Changes in asking permissions of Camera, microphone and Photo Library causing application to crash, with a list you can copy from in the answer at https://stackoverflow.com/a/40734360/968577

Without the required strings, your app will crash in this manner. However, the console output will explain what the problem is.

Community
  • 1
  • 1
Gary Makin
  • 3,109
  • 1
  • 19
  • 27
0

Try like this i hope it would be helpful!! Add these two points in info.plist first

  1. Privacy - Camera Usage Description
  2. Privacy - Photo Library Usage Description

Add these two delegates in your class file

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

Implement these delegate method

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

I just added exit(0) when i opened the camera settings. It's working fine

HariKarthick
  • 1,369
  • 1
  • 19
  • 47