Search code examples
iosswiftuiimagepickercontroller

How to use UIImagePickerController in swift 4


I am trying to get image from camera or photo library and use an UIImageView to preview that photo. I use the code below to capture image :

let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .camera
present(picker, animated: true, completion: nil)

and then I use this code to put image in imageView :

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

    imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}

I get permission and the app load successfully on my IPhone but when I capture a picture it doesn't go back to main View to show on image view . and it crash . can any body help me ?


Solution

  • You need to dismiss end of the function didFinishPickingMediaWithInfo

    picker.dismiss(animated:true,completion:nil)