PHPickerViewController load videos via PHAsset

僤鯓⒐⒋嵵緔 提交于 2020-12-15 05:49:16

问题


PHPickerViewController allows access to copies of photo library assets as well as returning PHAssets in the results. To get PHAssets instead of file copies, I do:

        let photolibrary = PHPhotoLibrary.shared()
        var configuration = PHPickerConfiguration(photoLibrary: photolibrary)
        configuration.filter = .videos
        configuration.selectionLimit = 0
        
        let picker = PHPickerViewController(configuration: configuration)
        picker.delegate = self
        self.present(picker, animated: true, completion: nil)

And then,

 //MARK:- PHPickerViewController delegate

@available(iOS 14, *)
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
    
    picker.dismiss(animated: true) {
        let identifiers:[String] = results.compactMap(\.assetIdentifier)
        let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: identifiers, options: nil)
        
        NSLog("\(identifiers), \(fetchResult)")
    }
    
}

But the problem is once the photo picker is dismissed, it prompts for Photo Library access which is confusing and since the user anyways implicitly gave access to the selected assets in PHPickerViewController, PHPhotoLibrary should load those assets directly. Is there anyway to avoid the Photo library permission?


回答1:


Is there anyway to avoid the Photo library permission?

Yes, sort of:

  1. Change PHPickerConfiguration(photoLibrary: photolibrary) to PHPickerConfiguration()

  2. Do not use the assetIdentifier to return to the photo library (it will be nil anyway)

In that case, the user can give you image data but that's all. The picker is out-of-process and no data from the library itself is really coming across, just a mere image that the user has explicitly selected.

However, if your goal really is to return to the photo library and obtain the PHAsset, then you must have permission, as you now are indeed attempting to probe the photo library behind the scenes within your app.

What I do, when my app depends on PHAsset information, is to ask for photo library permission (if needed) before presenting the picker, and I don't present the picker if I can't get permission. So, the user taps a button, I discover we have no permission, I ask for permission, we get it (let's say), and I present the picker (doing the asynchronous dance, because the permission arrives asynchronously). Looks great.

By the way, this is also just as true with UIImagePickerController.



来源:https://stackoverflow.com/questions/64223430/phpickerviewcontroller-load-videos-via-phasset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!