我创建了一个PHImagePicker
支持的应用程序。它可以工作,我可以从照片库中选择多张照片并将它们导入这个应用程序。但是当我重新打开这个时pickerController
,之前选择的图像带有蓝色标记。我必须先取消选择它们才能选择一些新图像。
我的问题是如何在每次打开 imagePickerController 时清除之前选择的图像状态?
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)
}
}
}
}
}
}
}
视图控制器
@objc func importButtonTapped(_ sender: UIButton) {
// ... omitted code
self.imagePicker.present(from: sender)
}