Alan Spark
Alan Spark

Reputation: 8302

UIImagePickerController throwing SIGABRT

I am experimenting with UIImagePickerController to allow photos to be selected from either the library or by taking a photo with the camera.

I followed the steps on a website (https://makeapppie.com/2016/06/28/how-to-use-uiimagepickercontroller-for-a-camera-and-photo-library-in-swift-3-0/) and got this working for the photo library but whenever I try to invoke the camera from my app it gives the error "Thread 1: signal SIGABRT".

This is the code that I am using to invoke the camera:

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

It was my understanding that the SIGABRT error would be expected inside the simulator. However when I tried it on my iPhone 7 I expected it to work and it gave the same error.

I have added the "Privacy - Camera Usage Description" to the Info.plist file.

Any ideas what I have done wrong?

Upvotes: 0

Views: 1353

Answers (1)

user7014451
user7014451

Reputation:

Here's my full code for using/selecting

// 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]
        }
    }
}

Ignore the two commented out lines - those are mine for testing things. This code works on all devices, iOS 9+, all orientations (but remember that iPhone is always displayed portrait), and I've never had any problems in either the simulator (it has no camera) nor a physical device.

Of interest is one thing that could cause an issue (not sure if it's throwing SIGABRT) - I'm checking for a rear camera and throwing up an alert if it doesn't exist. (No check for front camera though. I'm not even sure if anything but an iPod touch doesn't have a front facing camera.)

Also, don't forget to add two things to your info.plist for iOS 10:

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

You may put whatever you want in the description tags. Without these, the app will shut down in iOS 10 and Apple will reject your submission. Here's a link to more details.

Upvotes: 2

Related Questions