1

Here is my code, a simple class with a view created in storyboard that contains a button to present the imagePickerView. The imagePickerView gets presented and then the app crashes with libc++abi.dylib: terminating with uncaught exception of type NSException

import Foundation
import UIKit


class ImageSelectionView: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {


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

@IBAction func backButtonTapped(_ sender: AnyObject) {
    self.navigationController?.dismiss(animated: true, completion: nil)
}


@IBAction func openPhotoLibrary(_ sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
        imagePicker.allowsEditing = true
        present(imagePicker, animated: true, completion: nil)
        self.present(imagePicker, animated: true, completion: nil)
    }
}


func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    self.dismiss(animated: true, completion: nil);
}




}

Can't figure out where this is going wrong, any help would be awesome, thank you!

MikeG
  • 3,745
  • 1
  • 29
  • 51

3 Answers3

2

For accessing photo library, you need to put Privacy - Photo Library Usage Description in your application's .plist file with some description,

like this shown in image

enter image description here

And porticularly in your code, you have written code to present the same imagePicker twice as showing below.

    present(imagePicker, animated: true, completion: nil)
    self.present(imagePicker, animated: true, completion:

Hence I suggest to keep one, may be
present(imagePicker, animated: true, completion: nil)

OR

self.present(imagePicker, animated: true, completion:nil)

Hope it helps.

Happy coding ...

Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
0

Your syntax is wrong when U calling imagepicker delegate method then a viewcontroller is preseted to pick image So replace

present(imagePicker, animated: true, completion: nil)
self.present(imagePicker, animated: true, completion: nil)

WITH

self.presentViewController(imagePicker, animated: true, completion: nil)

and in you r not showing image in your imageview

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    "YOUR IMAGEVIEW".image = info[UIImagePickerControllerOriginalImage] as? UIImage
    self.dismiss(animated: true, completion: nil);
}
Jitendra Modi
  • 2,344
  • 12
  • 34
0

Try my code your problem will solve easily.

@IBAction func openPhotoLibrary(_ sender: AnyObject) {
        let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .Default) { action -> Void in
        if(  UIImagePickerController.isSourceTypeAvailable(.Camera))
        {
              let myPickerController = UIImagePickerController()
              myPickerController.delegate = self
              myPickerController.sourceType = .Camera
              self.presentViewController(myPickerController, animated: true, completion: nil)
        }else
        {
              let actionController: UIAlertController = UIAlertController(title: "Camera is not available",message: "", preferredStyle: .Alert)
              let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: .Cancel) { action -> Void in
                    //Just dismiss the action sheet
              }
              actionController.addAction(cancelAction)
              self.presentViewController(actionController, animated: true, completion: nil)

            }
        }

        actionSheetController.addAction(takePictureAction)
        //Create and add a second option action
        let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Camera Roll", style: .Default) { action -> Void in
            let myPickerController = UIImagePickerController()
            myPickerController.delegate = self;
            myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            self.presentViewController(myPickerController, animated: true, completion: nil)
        }
        actionSheetController.addAction(choosePictureAction)

        //Present the AlertController
        self.presentViewController(actionSheetController, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

        "YOURIMAGEVIEW".image = info[UIImagePickerControllerOriginalImage] as? UIImage

        self.dismissViewControllerAnimated(true, completion: nil)
}
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49