所以,我以前发现有人问过类似的问题,但当我尝试了与我得到的相同的方法时,它并没有像我预期的那样工作,所以我想做的是,我想从PHPickerViewController中从PHPicker Result中获得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
内部的代码没有被调用,这就是我无法获得图像的原因。
或者我的语法有误?有人能帮我吗?
如果以后需要资产,则必须使用照片库初始化配置。所以这一行是错的:
var configuration = PHPickerConfiguration()
您想要:
var configuration = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
matt可能说得对,但您还可以做以下事情。通过这种方式,图像将直接显示在UIImageView中,其配置与您所做的配置相同。
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
dismiss(animated: true)
let itemProviders = results.map(.itemProvider)
for item in itemProviders {
if item.canLoadObject(ofClass: UIImage.self) {
item.loadObject(ofClass: UIImage.self) { (image, error) in
DispatchQueue.main.async {
if let image = image as? UIImage {
self.myImageView.image = image
}
}
}
}
}
}
您可以使用这个自定义类ImagePickerManager:
import Foundation
import UIKit
import PhotosUI
class ImagePickerManager: NSObject, PHPickerViewControllerDelegate, UINavigationControllerDelegate {
var picker: PHPickerViewController?
var pickImageCallback : ((UIImage) -> ())?
var viewController: UIViewController?
override init(){
super.init()
}
func pickSingleImage(_ viewController: UIViewController, _ callback: @escaping ((UIImage) -> ())) {
pickImageCallback = callback
self.viewController = viewController
var configuration = PHPickerConfiguration()
configuration.filter = .any(of: [.images, .livePhotos])
configuration.selectionLimit = 1
picker = PHPickerViewController(configuration: configuration)
picker?.delegate = self
viewController.present(picker!, animated: true, completion: nil)
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true) {
let itemProviders = results.map(.itemProvider)
for item in itemProviders {
if item.canLoadObject(ofClass: UIImage.self) {
item.loadObject(ofClass: UIImage.self) { (image, error) in
DispatchQueue.main.async {
if let image = image as? UIImage {
self.pickImageCallback!(image)
}
}
}
}
}
}
}
}
用法:
在视图中,控制器初始化一个变量:
let imagePicker = ImagePickerManager()
在按钮动作上粘贴这行
ImagePickerManager().pickSingleImage(self) { pickedImage in
self.myImageView.image = pickedImage
}