PHPickerViewController:如何在重新打开图像选择器时取消选择之前选择的图像
PHPickerViewController: How to deselect the previous selected image when reopen the image picker
我创建了一个应用 PHImagePicker
支持。它有效,我可以 select 照片库中的多张照片并将它们导入此应用程序。但是当我重新打开这个 pickerController
时,之前 selected 的图像在那里带有蓝色标记。我必须先取消 select 它们才能 select 一些新图像。
我的问题是如何在每次打开 imagePickerController 时清除以前 selected 图像状态?
PHImagePicker
import UIKit
import PhotosUI
public protocol PHImagePickerDelegate: AnyObject {
func didSelect(images: [UIImage]?)
}
class PHImagePicker: NSObject {
private let pickerController: PHPickerViewController
private weak var presentationController: UIViewController?
private weak var delegate: PHImagePickerDelegate?
private var images = [UIImage]()
public init(presentationController: UIViewController, delegate: PHImagePickerDelegate) {
var config = PHPickerConfiguration()
config.selectionLimit = 3
// only show images
config.filter = PHPickerFilter.images
self.pickerController = PHPickerViewController(configuration: config)
super.init()
self.presentationController = presentationController
self.delegate = delegate
self.pickerController.delegate = self
}
private func pickerController(_ controller: PHPickerViewController, didSelect images: [UIImage]?) {
controller.dismiss(animated: true, completion: nil)
self.delegate?.didSelect(images: images)
}
public func present(from sourceView: UIView) {
self.presentationController?.present(pickerController, animated: true)
}
}
extension PHImagePicker: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
// clear images array
self.images = []
// dismiss the picker
picker.dismiss(animated: true, completion: nil)
print(picker)
print(results)
for result in results {
print("assetIdentifier", result.assetIdentifier ?? "")
print("itemProvider", result.itemProvider)
result.itemProvider.loadObject(ofClass: UIImage.self) { [weak self] object, error in
if let error = error {
print("ERROR: ", error)
return
}
print(object as Any)
if let image = object as? UIImage {
self?.images.append(image)
if self?.images != nil && self?.images.count == results.count {
DispatchQueue.main.async {
self?.pickerController(picker, didSelect: self?.images)
}
}
}
}
}
}
}
ViewController
@objc func importButtonTapped(_ sender: UIButton) {
// ... omitted code
self.imagePicker.present(from: sender)
}
您每次都在重复使用 PHImagePicker
的同一个实例:
self.imagePicker.present(from: sender)
这意味着您使用的是相同的 PHPickerController
,它具有相同的所选照片集。一个简单的修复方法是每次都创建一个新的 PHImagePicker
:
PHImagePicker(presentationController: self, delegate: self).present(from: sender)
您还可以重写 present
,以便重新创建 PHPickerController
。
我创建了一个应用 PHImagePicker
支持。它有效,我可以 select 照片库中的多张照片并将它们导入此应用程序。但是当我重新打开这个 pickerController
时,之前 selected 的图像在那里带有蓝色标记。我必须先取消 select 它们才能 select 一些新图像。
我的问题是如何在每次打开 imagePickerController 时清除以前 selected 图像状态?
PHImagePicker
import UIKit
import PhotosUI
public protocol PHImagePickerDelegate: AnyObject {
func didSelect(images: [UIImage]?)
}
class PHImagePicker: NSObject {
private let pickerController: PHPickerViewController
private weak var presentationController: UIViewController?
private weak var delegate: PHImagePickerDelegate?
private var images = [UIImage]()
public init(presentationController: UIViewController, delegate: PHImagePickerDelegate) {
var config = PHPickerConfiguration()
config.selectionLimit = 3
// only show images
config.filter = PHPickerFilter.images
self.pickerController = PHPickerViewController(configuration: config)
super.init()
self.presentationController = presentationController
self.delegate = delegate
self.pickerController.delegate = self
}
private func pickerController(_ controller: PHPickerViewController, didSelect images: [UIImage]?) {
controller.dismiss(animated: true, completion: nil)
self.delegate?.didSelect(images: images)
}
public func present(from sourceView: UIView) {
self.presentationController?.present(pickerController, animated: true)
}
}
extension PHImagePicker: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
// clear images array
self.images = []
// dismiss the picker
picker.dismiss(animated: true, completion: nil)
print(picker)
print(results)
for result in results {
print("assetIdentifier", result.assetIdentifier ?? "")
print("itemProvider", result.itemProvider)
result.itemProvider.loadObject(ofClass: UIImage.self) { [weak self] object, error in
if let error = error {
print("ERROR: ", error)
return
}
print(object as Any)
if let image = object as? UIImage {
self?.images.append(image)
if self?.images != nil && self?.images.count == results.count {
DispatchQueue.main.async {
self?.pickerController(picker, didSelect: self?.images)
}
}
}
}
}
}
}
ViewController
@objc func importButtonTapped(_ sender: UIButton) {
// ... omitted code
self.imagePicker.present(from: sender)
}
您每次都在重复使用 PHImagePicker
的同一个实例:
self.imagePicker.present(from: sender)
这意味着您使用的是相同的 PHPickerController
,它具有相同的所选照片集。一个简单的修复方法是每次都创建一个新的 PHImagePicker
:
PHImagePicker(presentationController: self, delegate: self).present(from: sender)
您还可以重写 present
,以便重新创建 PHPickerController
。