0

I can't find much documented regarding the PHPickerViewControllerDelegate didFinishPicking function using async await and I'm not sure I understand how to use the NSItemProvider. Here is my delegate code:

    func picker( _ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]){
    for result in results {
        Task {
            if result.itemProvider.hasItemConformingToTypeIdentifier(UTType.image.identifier) {
                do {
                    let url = try await result.itemProvider.loadItem(forTypeIdentifier: UTType.image.identifier) as! URL
                    print(url.description)
                    let data = try Data(contentsOf: url)
                    let image = UIImage(data: data)
                }
                catch {
                    print(error)
                }
            }
        }
    }
}

When used in the simulator the ItemProvider returns a URL but the data content of that URL is empty. How do I grab the image from the ItemProvider? Thanks

David
  • 305
  • 2
  • 10
  • If you want the data then I would use `loadDataRepresentation` instead of `loadItem`. – HangarRash Mar 01 '23 at 22:18
  • loadDataRepresentation is not available with async/await which I would prefer. What exactly does loadItem return? I don't necessarily need data, just the image and (if possible) metadata. – David Mar 03 '23 at 18:59
  • It's a shame only `loadItem` comes in the `async` form. To me that's the least clear method since there's no way to know what it's going to return. – HangarRash Mar 03 '23 at 19:34

0 Answers0