0

I have recently put in code to either take a picture, or select a picture, in Swift 3 on iOS 10. When calling either open camera or photo library, my app crashes with a "Thread 14: signal SIGABRT" with no other indication of what's happening. To me, and from what I've debugged, is this happens when trying to load the native camera view or photo library. Any help would be much appreciated!

Code below:

class CameraViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

@IBOutlet weak var imageView: UIImageView!


var imagePicker = UIImagePickerController()

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}


@IBAction func BTN_takePhoto(_ sender: AnyObject) {

    print("button pressed")

    if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)){

        print("source available")
        //imagePicker =  UIImagePickerController()
        print(1)
        imagePicker.delegate = self
        print(2)
        imagePicker.sourceType = UIImagePickerControllerSourceType.camera
        print(3)
        imagePicker.mediaTypes = [kUTTypeImage as String]
        print(4)
        imagePicker.allowsEditing = true
        print(5)
        self.present(imagePicker, animated: true, completion: nil)
        print(6)
    }
    else {
        print("ERROR: source not available")
        NSLog("No Camera")
        let alert = UIAlertController(title: "No camera", message: "Please allow this app the use of your camera in settings or buy a device that has a camera.", preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

@IBAction func BTN_pickFromLibrary(_ sender: AnyObject) {

    imagePicker.allowsEditing = false //2
    imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary //3
    present(imagePicker, animated: true, completion: nil)//4


}


func imagePickerController(imagePicker: UIImagePickerController, didFinishPickingMediaWithInfo info: NSDictionary!) {
    NSLog("Received image from camera")
    let mediaType = info[UIImagePickerControllerMediaType] as! String
    var originalImage:UIImage?, editedImage:UIImage?, imageToSave:UIImage?
    let compResult:CFComparisonResult = CFStringCompare(mediaType as NSString!, kUTTypeImage, CFStringCompareFlags.compareCaseInsensitive)
    if ( compResult == CFComparisonResult.compareEqualTo ) {

        editedImage = info[UIImagePickerControllerEditedImage] as! UIImage?
        originalImage = info[UIImagePickerControllerOriginalImage] as! UIImage?

        if ( editedImage != nil ) {
            imageToSave = editedImage
        } else {
            imageToSave = originalImage
        }
        imageView.image = imageToSave
        imageView.reloadInputViews()
    }
    imagePicker.dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    imagePicker.dismiss(animated: true, completion: nil)
}


/*
private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    imagePicker.dismiss(animated: true, completion: nil)
    imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
*/

}

1 Answers1

0

You have to add a key in Info.plist now from iOS 10 onwards

<key>NSPhotoLibraryUsageDescription</key>
<string>Use Photos</string>

enter image description here

Check this link for more details

Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98