Search code examples
iosswiftuiimagepickercontroller

everything in viewcontroller disappeared after I return from imagepicker?


when I am trying to implement all the uiimagepickerview methods in my app, my view controller (after choosing an image) shows nothing on it? It should have my textfields however they all 'disppeared'

Here's my code

@IBAction func registerUserProfileImageSelectButton(_ sender: UIButton) {
    imagePicker.allowsEditing = false
    imagePicker.sourceType = .photoLibrary
    present(imagePicker, animated: true, completion: nil)
}


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        registerUserProfileImage.contentMode = .scaleAspectFit
        registerUserProfileImage.image = pickedImage
    }
    dismiss(animated: true, completion: nil)
}


func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}

I suspect it may be something to do in my dismiss method, but I am not too certain

Thanks for your help in advance!


Solution

  • func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
    

    The problem is you are trying to dismiss the current view controller, not the image picker controller. Please use:

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

    That should work!