在WWDC20中,苹果推出了PHPicker -- UIImagePickerController的现代替代品。
我想知道是否可以使用新的照片选择器检索PHAsset?
这里是我的代码
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)
}发布于 2020-06-28 17:11:59
我在苹果论坛上设法从这个框架的开发人员那里找到了答案。
是的,PHPickerResult具有assetIdentifier属性,该属性可以包含一个本地标识符,以便从库中获取PHAsset。要让PHPicker返回资产标识符,需要使用库初始化PHPickerConfiguration。 请注意,如果用户将应用程序置于有限照片库模式中,则PHPicker不会为所选项目扩展有限照片库访问权限。这将是一个很好的机会重新考虑,如果应用程序真的需要直接的照片库访问,或可以只处理图像和视频数据。但这真的取决于应用程序。 “满足新照片选择器”会话的相关部分从10米20开始。
用于访问的PhotoKit示例代码如下所示:
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
}
}https://stackoverflow.com/questions/62625797
复制相似问题