So, I found similar questions asked before but when I tried the same way as what I got, and it does not work as I expected, so what I'm trying to do is, I want to get PHAsset from PHPickerResult from PHPickerViewController.
so experimenting it using this source as my base code, and combined it with what I got from this.
also already add
"Privacy - Photo Library Usage Description"
to the info.plist
when i tried this.
The code look like this.
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
}
}
}
}
After I try to debug the code, I found out that the code inside fetchResult.enumerateObjects
was not called that's why I can't get the image.
Or maybe I got the wrong syntax? can someone help me?
If you need an asset later, you have to initialize the configuration with the photo library. So this line is wrong:
var configuration = PHPickerConfiguration()
You want:
var configuration = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())