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.