I want to select multiple images. Currently I was using UIImagePickerController
. However, this does not allow multi-image selection as per this question. So I'm looking into implementing this using the PHPickerViewController
. However, to progress I need the image URL, as these selected images will be later uploaded. In UIImagePickerController
it was fairly straightforward to get the url.
Example
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let imageURL = info[UIImagePickerController.InfoKey.imageURL] as? URL {
// some logic
} else {
// error handling logic
}
}
I can't find any ways to achieve this using the PHPickerViewController
. The closest I found was this question. But using this results in an invalid url. Which while uploading suggests the given image is empty.
Current logic as it stands is:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
guard !results.isEmpty else {
picker.dismiss(animated: true)
return
}
for item in results {
let itemProvider = item.itemProvider
itemProvider.loadFileRepresentation(forTypeIdentifier: "public.item") { (url, error) in
if error != nil {
// Error handling
} else {
if let url = url {
// Got the URL but its wrong
}
}
}
}