How to retrieve PHAsset from PHPickerViewController?
Asked Answered
S

1

5

In WWDC20 apple introduced PHPickerViewController - the modern replacement for UIImagePickerController.
I'm wondering if it's possible to retrieve PHAsset using the new photo picker?


Here is my code:

private func presentPicker(filter: PHPickerFilter) {
    var configuration = PHPickerConfiguration()
    configuration.filter = filter
    configuration.selectionLimit = 0
    
    let picker = PHPickerViewController(configuration: configuration)
    picker.delegate = self
    present(picker, animated: true)
}


func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
    dismiss(animated: true)
}
Selfcongratulation answered 28/6, 2020 at 17:11 Comment(0)
S
27

I managed to find an answer from the developers of this framework on the apple forum:

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:

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
    }
}
Selfcongratulation answered 28/6, 2020 at 17:11 Comment(6)
Thanks for reporting this, good use of self Q and A on SO.Matrona
Is it possible to obtain some meta data (like photo shot date) directly from the ItemProvider? Or as long as we need the meta, we need go through the PHAsset?Nysa
Is anyone else running into an issue where the fetchResult is always empty? The compactMap works and I get identifiers back, but the fetch result never returns any results for me.Cadal
@JKoko remember set PHPhotoLibrary.shared() in PHPickerConfigurationWamsley
I'm running into the same issue. @JKoko have you found a solution?Moffat
@JKoko It seems that you need access to All Photos, i.e. full permission in order to access a PHAsset.Cypro

© 2022 - 2024 — McMap. All rights reserved.