I am able to get image data using the new PhotosPicker in iOS 16 / macOS Ventura. Here is what I do:
PhotosPicker(selection: $selectedPhotosPickerItems, matching: .any(of: [.images]), photoLibrary: .shared()) {
Text("Select photos")
}
.onChange(of: selectedPhotosPickerItems) { newItems in for newItem in newItems {
Task {
if let data = try? await newItem.loadTransferable(type: Data.self) {
... = UIImage(data: data)
}
}
}
}
However, I need some more information about the photo. If I could fetch the PHAsset somehow, that would be awesome or at least the original creation date to start with. PHAsset does not conform to Transferable. Is there maybe any way to fetch PHAsset only knowing the image data? Maybe with a hash?
CodePudding user response:
Xcode 14 / SwiftUI 4
We have identifier in result item for that, so it is possible to fetch corresponding PHAsset
.
Here is a simplified demo for single selection item (assuming Info.plist is updated and authorisation request done).
.onChange(of: selectedPhotosPickerItem) { newItem in
if let newItem, let localID = newItem.itemIdentifier {
let result = PHAsset.fetchAssets(withLocalIdentifiers: [localID], options: nil)
if let asset = result.firstObject {
print("Got " asset.debugDescription)
}
*iOS/macOS approaches are the same here