SwiftUI PhotosPicker does not work

iOS 17.4.1, iPhone 15 Pro.

I pick photos from the user's photo library using:

...
.photosPicker(isPresented: $addPhotos, selection: $pickedPhotos, matching: .images)
.onChange(of: pickedPhotos) {
    import(photoItems: pickedPhotos)
}

The picker UI works ok, but then when I import the photos:

private func import(photoItems: [PhotosPickerItem]) {
    for photoItem in photoItems {
        Log.debug("picked: \(photoItem)")
        Task {
            do {
                let imageData = try await photoItem.loadTransferable(type: Data.self)
                    
                guard let imageData else {
                    Log.error("failed to load image data")
                    return
                }
                    
                guard let image = UIImage(data: imageData) else {
                    Log.error("failed to create image from data")
                    return
                }
                    
                // use image
                ....
            }
            catch {
                Log.error("failed to load image data: \(error)")
            }
        }
    }
}

Logging the picked photo gives:

PhotosPickerItem(_itemIdentifier: "C7E2F753-43F6-413D-BA42-509C60BE9D77/L0/001", _shouldExposeItemIdentifier: false, _supportedContentTypes: [<_UTCoreType 0x1ebcd1c10> public.jpeg (not dynamic, declared), <_UTCoreType 0x1ebcd1d70> public.heic (not dynamic, declared), <UTType 0x300fe0430> com.apple.private.photos.thumbnail.standard (not dynamic, declared), <UTType 0x300fe03f0> com.apple.private.photos.thumbnail.low (not dynamic, declared)], _itemProvider: <PUPhotosFileProviderItemProvider: 0x303fdff00> {types = (
    "public.jpeg",
    "public.heic",
    "com.apple.private.photos.thumbnail.standard",
    "com.apple.private.photos.thumbnail.low"
)})

Looks like there's a valid photo? But then the loadTransferable() call fails with:

5C9D59CB-3606-48C1-9B37-1F18D642B3AD grantAccessClaim reply is an error: Error Domain=NSCocoaErrorDomain Code=4101 "Couldn’t communicate with a helper application." UserInfo={NSUnderlyingError=0x308244f30 {Error Domain=PFPAssetRequestErrorDomain Code=0 "The operation couldn’t be completed. (PFPAssetRequestErrorDomain error 0.)" UserInfo={NSURL=file:///private/var/mobile/Containers/Shared/AppGroup/36CF50FB-38FC-440E-9662-35C23B5E636C/File%20Provider%20Storage/photospicker/uuid=C7E2F753-43F6-413D-BA42-509C60BE9D77&library=1&type=1&mode=2&loc=true&cap=true.jpeg, NSLocalizedDescription=The operation couldn’t be completed. (PFPAssetRequestErrorDomain error 0.)}}}
Error loading
public.data:

Error Domain=NSItemProviderErrorDomain Code=-1000 "Cannot load representation of type public.jpeg" UserInfo={NSLocalizedDescription=Cannot load representation of type public.jpeg, NSUnderlyingError=0x3081a2550 {Error Domain=NSCocoaErrorDomain Code=4101 "Couldn’t communicate with a helper application." UserInfo={NSUnderlyingError=0x308244f30 {Error Domain=PFPAssetRequestErrorDomain Code=0 "The operation couldn’t be completed. (PFPAssetRequestErrorDomain error 0.)" UserInfo={NSURL=file:///private/var/mobile/Containers/Shared/AppGroup/36CF50FB-38FC-440E-9662-35C23B5E636C/File%20Provider%20Storage/photospicker/uuid=C7E2F753-43F6-413D-BA42-509C60BE9D77&library=1&type=1&mode=2&loc=true&cap=true.jpeg, NSLocalizedDescription=The operation couldn’t be completed. (PFPAssetRequestErrorDomain error 0.)}}}}}

2024-04-03 12:16:07.8010 error PhotosView.import: failed to load image data: importNotSupported
[

As usual I rebooted my phone as these things tend to be pretty buggy in iOS, but same error. Note this is not in a simulator which seems to have long standing bugs related to photo picking, this is on a freshly upgraded 17.4.1 device.

I can't find any documentation related to these errors and all googling comes up with a few other cases but no solutions. Should this API actually work or is it better to go back to the old UIKit stuff? I use loadTransferable(type: Data.self) as UIImage.self is not Transferable and this hack has seemed to work ok for some months.

Post not yet marked as solved Up vote post of null_pointer Down vote post of null_pointer
282 views

Replies

The code snippet looks correct to me. Please submit a Feedback with a sysdiagnose (and let us know the feedback ID) so we can investigate the issue further. Thanks!

... and now it works again. The phone was disconnected and when I connected it to gather the sysdiagnose the code works as it should. Maybe something restarted the "helper application". Yesterday I did probably 30 tests and nothing worked. If/when it appears again I'll do the feedback.

provider.loadItem(forTypeIdentifier: "public.movie") { url, error in guard let videoURL = url as? URL else { debugPrint("Error loading video URL: (error?.localizedDescription ?? "")") return } }

I am using Apple photo library and trying to load video using above code but it gives me url like this: file:///private/var/mobile/Containers/Shared/AppGroup/82DEA83D-2D23-458E-8B7A-7B47924BF3BA/File%20Provider%20Storage/photospicker/uuid=E1C42B07-6A92-4A15-A324-03CDA69447C0&library=1&type=3&mode=1&loc=true&cap=true.mov

And once i move to upload process than i get error: provided url is a directory.

Any thoughts, is i'm doing something wrong or its a bug?

Thanks

Any update on this? My users are experiencing the same issue