//ads:
?>
Can I set the order of the images? PHPickerViewController
PHP Snippet 1:
func makeUIViewController(context: Context) -> PHPickerViewController {
var config = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
config.filter = .images
config.selection = .ordered
config.selectionLimit = 0
let picker = PHPickerViewController(configuration: config)
picker.delegate = context.coordinator
return picker
}
PHP Snippet 2:
class Coordinator: NSObject, PHPickerViewControllerDelegate {
let parent:ImagePicker
init(_ parent:ImagePicker) {
self.parent = parent
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true)
let group = DispatchGroup()
var images = [UIImage]()
var order = [String]()
var asyncDict = [String:UIImage]()
for result in results {
order.append(result.assetIdentifier ?? "")
group.enter()
let provider = result.itemProvider
if provider.canLoadObject(ofClass: UIImage.self) {
provider.loadObject(ofClass: UIImage.self) { image, _ in
guard let updatedImage = image as? UIImage else {group.leave();return}
asyncDict[result.assetIdentifier ?? ""] = updatedImage
group.leave()
}
}
}
group.notify(queue: .main) {
for id in order {
images.append(asyncDict[id]!)
}
self.parent.images = images
}
}
}