I ViewController trying to transfer a picture from the gallery to a new ViewController , but an error occurs

Unexpectedly found a nil while unwrapping an optional value

Code:

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { if let im = info[UIImagePickerControllerOriginalImage] as? UIImage { let vc = self.storyboard?.instantiateViewController(withIdentifier: "editImage") as! EditImageViewController vc.imageView.image = im self.present(vc, animated: true, completion: nil) } else { print("Something went wrong") } imagePicker.dismiss(animated: true, completion: nil) } 

Where is the mistake?

    1 answer 1

    Try to take a picture like this

     class EditImageViewController: UIViewController { @IBOutlet weak var imageView: UIImageView! var image: UIImage! override func viewDidLoad() { super.viewDidLoad() imageView.image = image } } 

    And transmit so

     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { if let im = info[UIImagePickerControllerOriginalImage] as? UIImage { let vc = storyboard?.instantiateViewController(withIdentifier: "editImage") as! EditImageViewController vc.image = im present(vc, animated: true, completion: nil) } else { print("Something went wrong") } imagePicker.dismiss(animated: true, completion: nil) } 

    Because it looks like you are trying to insert an image into a still inaccessible imageView.