PHPickerViewController fails to load image

I have been trying to load an image from the photo library on a swifui app. I am running Xcode 13.2.1 and building IOS 15.2 My code is as below


    @Binding var image: UIImage?

    func makeUIViewController(context: Context) -> PHPickerViewController {

        var config = PHPickerConfiguration()

        config.filter = .images

        config.selectionLimit = 1

        config.preferredAssetRepresentationMode = .compatible

        let controller = PHPickerViewController(configuration: config)

        controller.delegate = context.coordinator

        return controller

    }

    func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) { }

    func makeCoordinator() -> Coordinator {

        Coordinator(self)

    }
    // Use a Coordinator to act as your PHPickerViewControllerDelegate

    class Coordinator: NSObject, PHPickerViewControllerDelegate {
        private let parent: PhotoPicker

        init(_ parent: PhotoPicker) {

            self.parent = parent

        }
        func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {

            picker.dismiss(animated: true)

            print(results)
            guard !results.isEmpty else {
                return
            }

            guard let itemProvider = results.first?.itemProvider else { return }

            print("Invoking getPhoto")

            self.getPhoto2(from: itemProvider)

            //parent.didFinishPicking(!results.isEmpty)

        }

        private func getPhoto2(from itemProvider: NSItemProvider) {

            print("getPhoto")
         
            if itemProvider.canLoadObject(ofClass: UIImage.self) {

                itemProvider.loadObject(ofClass: UIImage.self) { image, error in

                    self.parent.image = image as? UIImage

                    print("Loaded Image \(error)")

                }

            }

        }
}
}

On the console I see the following error

022-01-27 00:40:01.485085-0500 Vescense[3174:884964] [Picker] Showing picker unavailable UI (reason: still loading) with error: (null)

Further when I print the result I see

[PhotosUI.PHPickerResult(itemProvider: <PUPhotosFileProviderItemProvider: 0x2818fc980> {types = (

    "public.jpeg",

    "public.heic"

)}, assetIdentifier: nil)]

It doesn't appear like the error on loadObject has a value. And it's suspicious that assetIdentifier is nil.

Any thoughts on what I might be missing here would be most helpful.

UPDATE:

I tried using loadFileRepresentation instead.

itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.jpeg.identifier) { (url, error) in guard let url = url else { print("Failed to load url") return }

And this time I get the following error on the console.

`Error copying file type public.jpeg. Error: Error Domain=NSItemProviderErrorDomain Code=-1000 "Cannot load representation of type public.jpeg" UserInfo={NSLocalizedDescription=Cannot load representation of type public.jpeg, NSUnderlyingError=0x600001d26430 {Error Domain=NSCocoaErrorDomain Code=4101 "Couldn’t communicate with a helper application." UserInfo={NSUnderlyingError=0x600001dbcb70 {Error Domain=PHAssetExportRequestErrorDomain Code=0 "The operation couldn’t be completed. (PHAssetExportRequestErrorDomain error 0.)" UserInfo={NSLocalizedDescription=The operation couldn’t be completed. (PHAssetExportRequestErrorDomain error 0.), NSUnderlyingError=0x600001dbca80 {Error Domain=PAMediaConversionServiceErrorDomain Code=2 "The operation couldn’t be completed. (PAMediaConversionServiceErrorDomain error 2.)" UserInfo=0x600001398e60 (not displayed)}}}}}}`

It's a known issue that the red flower image can't be selected using the simulator (63426347).

The issue only affects the simulator environment so it should work on actual hardwares. If you want to continue using the simulator to test, you can select other images instead.

I'll try this on the simulator as well.

Same problem here. I'm running in a iPhone X with iOS 15.3.1

I have two photos in the same folder. The first fails when result.itemProvider.loadObject(ofClass: UIImage.self) [0] (null) "public.jpeg" : "/private/var/mobile/Containers/Shared/AppGroup/F925A8FA-C8EC-42CB-8990-0370E73A9808/File Provider Storage/photospicker/version=1&uuid=3034711E-F03B-44CD-B180-659775A56EA6&mode=current.jpeg"

The second is ok. [0] (null) "public.jpeg" : "/private/var/mobile/Containers/Shared/AppGroup/F925A8FA-C8EC-42CB-8990-0370E73A9808/File Provider Storage/photospicker/version=1&uuid=299A6E28-21CC-440E-8C1B-A05F479081C0&mode=current.jpeg"

I have tried also with result.itemProvider.loadFileRepresentation(forTypeIdentifier: representation), with UTType.jpeg.identifier, and the same error.

PHPickerResult(itemProvider: <PUPhotosFileProviderItemProvider: 0x281f4a680> {types = ( "public.jpeg" )}, assetIdentifier: Optional("3034711E-F03B-44CD-B180-659775A56EA6/L0/001")) 2022-02-24 23:45:49.484182+0100 App[1344:394073] [claims] Upload preparation for claim 8E1CBDF5-66F2-4453-87A1-2AB03E93F974 completed with error: Error Domain=NSCocoaErrorDomain Code=260 "The file “version=1&uuid=3034711E-F03B-44CD-B180-659775A56EA6&mode=current.jpeg” couldn’t be opened because there is no such file." UserInfo={NSURL=file:///private/var/mobile/Containers/Shared/AppGroup/F925A8FA-C8EC-42CB-8990-0370E73A9808/File%20Provider%20Storage/photospicker/version=1&uuid=3034711E-F03B-44CD-B180-659775A56EA6&mode=current.jpeg, NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/F925A8FA-C8EC-42CB-8990-0370E73A9808/File Provider Storage/photospicker/version=1&uuid=3034711E-F03B-44CD-B180-659775A56EA6&mode=current.jpeg, NSUnderlyingError=0x283fe1290 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

I am getting the exact same error - though the request type is public.camera-raw-image - when calling loadFileRepresentation with a RAF file - Fujifilm XT-4 raw image file. Any update on this? Is this an iOS issue?

It looks like directly loading data for UTType.heic does work for the red flower photo. I've used that as a fallback to ensure all photos work in the sim.

PHPickerViewController fails to load image
 
 
Q