PHPickerViewController 允许访问照片库资产的副本以及在结果中返回 PHAsset。要获取 PHAssets 而不是文件副本,我会:
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)
接着,
//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)")
}
}
但问题是,一旦照片选择器被关闭,它会提示对照片库的访问,这令人困惑,并且由于用户无论如何都隐含地授予了对 PHPickerViewController 中选定资产的访问权限,PHPhotoLibrary 应该直接加载这些资产。有没有办法避免照片库权限?