[英]PHPickerViewController's Cancel Button is not working in iOS 15
I am using PHPickerViewController
to pick Image for User Profile Picture Purpose in iOS 15. I am using UIKit framework.我正在使用
PHPickerViewController
在 iOS 15 中为用户个人资料图片目的选择图像。我正在使用 UIKit 框架。 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. Picker 在选择图像和委派结果方面工作正常。 But, when the
Cancel
button is pressed, nothing happens and the Picker is not dismissed as expected.但是,当按下
Cancel
按钮时,什么都没有发生,并且 Picker 没有按预期解除。
How to dismiss the PHPickerViewController
instance when its own Cancel
button is pressed ?当
PHPickerViewController
自己的Cancel
按钮被按下时,如何关闭它?
Edit:编辑:
The implementation of PHPickerViewControllerDelegate
Method is as follows: PHPickerViewControllerDelegate
方法的实现如下:
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()
}
}
}
}
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.您需要在用户完成选择或点击取消按钮时调用的
picker(_:didFinishPicking:)
委托方法中自行关闭选择器。
From the Apple docs for picker(_:didFinishPicking:)
:来自
picker(_:didFinishPicking:)
的Apple docs :
The system doesn’t automatically dismiss the picker after calling this method.
For example:例如:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
// Do something with the results here
picker.dismiss(animated: true)
}
Your current delegate code only calls picker.dismiss
when the results array is non-empty (ie when the user has selected images).您当前的委托代码仅在结果数组非空时(即当用户选择图像时)调用
picker.dismiss
。 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:通过在委托方法的代码顶部添加以下内容来解决此问题:
if results.isEmpty {
picker.dismiss(animated: true)
return
}
you just wrap it out in an objc func for making cancel button works您只需将其包装在 objc 函数中即可使取消按钮起作用
@objc
func didOpenPhotos() {
lazy var pickerConfig = PHPickerConfiguration()
pickerConfig.filter = .images
pickerConfig.selectionLimit = 1
let pickerView = PHPickerViewController(configuration: pickerConfig)
pickerView.delegate = self
self.present(pickerView, animated: true)
}
call it anywhere在任何地方调用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.