I am using PHPickerViewController
to pick Image for User Profile Picture Purpose in iOS 15. I am using UIKit framework. I have the following code:
var pickerConfig = PHPickerConfiguration(photoLibrary: .shared())
pickerConfig.selectionLimit = 1
pickerConfig.filter = .images
let pickerView = PHPickerViewController(configuration: pickerConfig)
pickerView.delegate = self
self.present(pickerView, animated: true)
The Picker is working properly for selecting images and delegating the results. But, when the Cancel
button is pressed, nothing happens and the Picker is not dismissed as expected.
How to dismiss the PHPickerViewController
instance when its own Cancel
button is pressed ?
Edit:
The implementation of PHPickerViewControllerDelegate
Method is as follows:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult])
{
results.first?.itemProvider.loadObject(ofClass: UIImage.self) { [unowned self] reading , error in
guard let image = reading as? UIImage, error == nil else
{
DispatchQueue.main.async {
picker.dismiss(animated: true)
self.profilePictureHasError = true
self.toggleDoneButtonEnabled()
}
return
}
self.profilePictureHasError = false
DispatchQueue.main.async {
picker.dismiss(animated: true)
self.profilePictureHasChanged = self.userProfilePicture != image
if self.profilePictureHasChanged
{
self.profilePictureView.image = image
self.toggleDoneButtonEnabled()
}
}
}
}
2
Answers
you just wrap it out in an objc func for making cancel button works
call it anywhere
You need to dismiss the picker yourself in the
picker(_:didFinishPicking:)
delegate method which is called when the user completes a selection or when they tap the cancel button.From the Apple docs for
picker(_:didFinishPicking:)
:For example:
Your current delegate code only calls
picker.dismiss
when the results array is non-empty (i.e when the user has selected images). When the cancel button is tapped, the delegate method is called with an empty results array.Fix the issue by adding the following to the top of the code in your delegate method: