Isaac
Isaac

Reputation: 218

imagePickerController error selecting a Photo

I'm using imagePickerController, but when i select an imagen got an Error:

fatal error: unexpectedly found nil while unwrapping an Optional value

i dont know why because im unwrapping the value with the if let

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

    if let img = info[UIImagePickerControllerOriginalImage] as? UIImage {
        photoImageView.image = img
    }

    dismissViewControllerAnimated(true, completion: nil)
}

Upvotes: 0

Views: 336

Answers (2)

nameCensored
nameCensored

Reputation: 86

Does the error occurs at the line

photoImageView.image = img

In this case it might be that photoImageView is nil (it is often an implicitly unwrapped optional).

Updated from comments:

Please check that photoImageView is an @IBOutlet and it is connected to an UIImageView on the storyboard. If you are creating it programmatically, try adding it to a view in the window before setting the image.

Upvotes: 2

SwiftStudier
SwiftStudier

Reputation: 2324

Maybe anything wrong with VC calling. This code works fine:

let picker = UIImagePickerController()
            picker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            picker.allowsEditing  = false
            picker.delegate = self
            self.presentViewController(picker, animated: true, completion: nil)



func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    productImageView.image = image
    dismissViewControllerAnimated(true, completion: nil)
}

Upvotes: 0

Related Questions