gpt4 book ai didi

ios - PHPickerViewController 的取消按钮在 iOS 15 中不起作用

转载 作者:行者123 更新时间:2023-12-02 18:07:23 67 4
gpt4 key购买 nike

我正在使用 PHPickerViewController 在 iOS 15 中为用户个人资料图片选择图像。我正在使用 UIKit 框架。我有以下代码:

var pickerConfig = PHPickerConfiguration(photoLibrary: .shared())
pickerConfig.selectionLimit = 1
pickerConfig.filter = .images
let pickerView = PHPickerViewController(configuration: pickerConfig)
pickerView.delegate = self
self.present(pickerView, animated: true)

选择器在选择图像和委托(delegate)结果方面正常工作。但是,当按下 Cancel 按钮时,没有任何反应,而且 Picker 没有按预期关闭。

如何在按下 Cancel 按钮时关闭 PHPickerViewController 实例?

编辑:

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()
}
}
}
}

最佳答案

您需要在 picker(_:didFinishPicking:) 委托(delegate)方法中自行关闭选择器,该方法在用户完成选择点击取消时调用按钮。

来自Apple docs对于 picker(_:didFinishPicking:):

The system doesn’t automatically dismiss the picker after calling this method.

例如:

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
// Do something with the results here
picker.dismiss(animated: true)
}

您当前的委托(delegate)代码仅在结果数组为非空时(即当用户选择了图像时)调用 picker.dismiss。点击取消按钮时,将使用空结果数组调用委托(delegate)方法。

通过将以下内容添加到委托(delegate)方法中的代码顶部来解决此问题:

if results.isEmpty {
picker.dismiss(animated: true)
return
}

关于ios - PHPickerViewController 的取消按钮在 iOS 15 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72976165/

67 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com