Can you retrieve the PHAsset using the New Photo Picker?

I'm currently using the UIImagePickerController to get images, but I also save the PHAsset's identifier so I can reopen the last image viewed when the app is reopened. Is this available in the PHPickerResult's itemProvider? Or is it the assetIdentifier that's part of the PHPickerResult?

Replies

Yes, PHPickerResult has the assetIdentifier property which can contain a local identifier to fetch the PHAsset from the library.
To have PHPicker return asset identifiers, you need to initialize PHPickerConfiguration with the library.

Please note that PHPicker does not extend the Limited Photos Library access for the selected items if the user put your app in Limited Photos Library mode.
It would be a good opportunity to reconsider if the app really needs direct Photos Library access or can work with just the image and video data. But that really depend on the app.

The relevant section of the "Meet the new Photos picker" session begins at 10m 20s.

Sample code for PhotoKit access looks like this:

Code Block swift
import UIKit
import PhotosUI
class PhotoKitPickerViewController: UIViewController, PHPickerViewControllerDelegate {
@IBAction func presentPicker(_ sender: Any) {
let photoLibrary = PHPhotoLibrary.shared()
let configuration = PHPickerConfiguration(photoLibrary: photoLibrary)
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
present(picker, animated: true)
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true)
let identifiers = results.compactMap(\.assetIdentifier)
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: identifiers, options: nil)
// TODO: Do something with the fetch result if you have Photos Library access
}
}


Thank you for this guidance! I've been following this to try and get the PHAsset, however I've noticed that the local identifiers are always nil and it seems like others are finding the same (like this blog I found ikyle.me/blog/2020/phpickerviewcontroller). Is this a bug on the radar you guys are trying to fix? My app orders photos based on the date they were taken so being able to accomplish this is important to us!
You need to use PHPickerConfiguration(photoLibrary: photoLibrary) instead of PHPickerConfiguration(). As listed in the header docs, PHPickerConfiguration() never returns asset identifiers.
The Objective-C way to get the assetIdentifier would be something like
Code Block
-(void)selectPressed:(id)sender{
PHPhotoLibrary *photoLibrary = [PHPhotoLibrary sharedPhotoLibrary];
PHPickerConfiguration *config = [[PHPickerConfiguration alloc] initWithPhotoLibrary: photoLibrary];
config.selectionLimit = 1;
config.filter = [PHPickerFilter videosFilter];
PHPickerViewController *pickerViewController = [[PHPickerViewController alloc] initWithConfiguration:config];
pickerViewController.delegate = self;
[self presentViewController:pickerViewController animated:YES completion:nil];
}
-(void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPickerResult *> *)results{
[picker dismissViewControllerAnimated:YES completion:nil];
for (PHPickerResult *result in results) {
NSArray *assetIdentifiers = @[result.assetIdentifier];
PHFetchResult *assetResults = [PHAsset fetchAssetsWithLocalIdentifiers:assetIdentifiers options:nil];
// Do something with the assetResults if you have Photos Library access
}
}


It would be a good opportunity to reconsider if the app really needs direct Photos Library access or can work with just the image and video data.

Is there any way to get the creation date of a video in the Photos Library without having direct access to it? The creation date of the picked result is not necessarily the same. Examples in a github project:

https://github.com/Jan-E/ikyle.me-code-examples/blob/master/Photo%20Picker%20ObjC/Photo%20Picker%20ObjC/PHPickerController.m#L223


Is there any way to get the creation date of a video in the Photos Library without having direct access to it? The creation date of the picked result is not necessarily the same. Examples in a github project:

https://github.com/Jan-E/ikyle.me-code-examples/blob/master/Photo%20Picker%20ObjC/Photo%20Picker%20ObjC/PHPickerController.m#L223

You can read the video file's metadata to get the creation date after loading the file using the item provider.