1
votes

I just wanted to click a picture and display it on UIImageView.

I had defined all needed outlets and actions and here is the code.

But it shows the error.

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var camera: UIButton!

    @IBOutlet weak var gallery: UIButton!

    @IBOutlet weak var viewImage: UIImageView!

    @IBAction func PhotoGalleryAction(sender: UIButton) {

        let picker = UIImagePickerController()

        picker.delegate = self

        picker.sourceType = .PhotoLibrary

        presentViewController(picker, animated: true, completion: nil)
    }



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

        viewImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage

        dismissViewControllerAnimated(true, completion: nil)

    }
}

Here is the error:

fatal error: unexpectedly found nil while unwrapping an Optional value
1
Did you connect the image view outlets ?Midhun MP
Ohh, that was a pretty bad mistake. I had connected but somehow it wasn't linked. Thank you.Jay Patel

1 Answers

1
votes

You have to check the connections between storyboard and code. Even if you already connected it, it may be corrupted by using an old storyboard file or something else (it often happens when using the Assitant Editor)