所以,我发现之前问过类似的问题,但是当我尝试与我得到的相同的方式时,它并没有按我的预期工作,所以我想做的是,我想从 PHPickerViewController 的 PHPickerResult 获取 PHAsset。
所以使用这个源代码作为我的基本代码来试验它,并将它与我从中得到的.
info.plist
当我尝试这个时,也已经添加了“隐私 - 照片库使用说明” 。
代码如下所示。
import UIKit
import PhotosUI
class ViewController: UIViewController {
@IBOutlet weak var myImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func buttonDidTap(_ sender: Any) {
var configuration = PHPickerConfiguration()
configuration.selectionLimit = 1
configuration.filter = .any(of: [.images, .videos])
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
self.present(picker, animated: true, completion: nil)
}
}
extension ViewController: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true)
let identifiers = results.compactMap(\.assetIdentifier)
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: identifiers, options: nil)
fetchResult.enumerateObjects { (asset, index, stop) -> Void in
PHImageManager.default().requestImage(for: asset,
targetSize: CGSize.init(width: 20, height: 20),
contentMode: PHImageContentMode.aspectFit,
options: nil) { (image: UIImage?, _: [AnyHashable : Any]?) in
self.myImageView.image = image
}
}
}
}
在我尝试调试代码后,我发现里面的代码fetchResult.enumerateObjects
没有被调用,这就是我无法获取图像的原因。
或者我的语法错误?有人能帮我吗?