Ios 使用PHPickerViewController将照片传递到集合视图
我正在尝试使用PHPickerViewController从选择器获取照片并将其添加到集合视图。由于PHPickerViewController是如此之新,我发现关于它的信息非常有限。我假设我必须将其转换为PHAsset,但我没有运气这样做。下面的代码将无错误运行,但集合视图显示为空。我知道我缺少了一些在picker委托中处理结果的代码,这是因为我尝试的每个解决方案都失败了。我想我还需要在cellForItem委托中处理fetchResult,但我在这方面也失败了。关于我做错了什么,或者在哪里可以找到解决方法,有什么想法吗 收藏视图Ios 使用PHPickerViewController将照片传递到集合视图,ios,swift,uicollectionview,phpickerviewcontroller,Ios,Swift,Uicollectionview,Phpickerviewcontroller,我正在尝试使用PHPickerViewController从选择器获取照片并将其添加到集合视图。由于PHPickerViewController是如此之新,我发现关于它的信息非常有限。我假设我必须将其转换为PHAsset,但我没有运气这样做。下面的代码将无错误运行,但集合视图显示为空。我知道我缺少了一些在picker委托中处理结果的代码,这是因为我尝试的每个解决方案都失败了。我想我还需要在cellForItem委托中处理fetchResult,但我在这方面也失败了。关于我做错了什么,或者在哪里可
class DetailViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UINavigationControllerDelegate, PHPickerViewControllerDelegate {
var editCollectionView: UICollectionView!
var photosInTheCellNow = [UIImage]()
let configuration = PHPickerConfiguration()
var imageView: UIImageView!
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
var config = PHPickerConfiguration()
config.selectionLimit = 10
config.filter = PHPickerFilter.images
let picker = PHPickerViewController(configuration: config)
picker.delegate = self
present(picker, animated: true)
let layout = UICollectionViewFlowLayout()
editCollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
editCollectionView.delegate = self
editCollectionView.dataSource = self
editCollectionView.backgroundColor = UIColor.white
editCollectionView.allowsMultipleSelection = true
editCollectionView.register(ImageEditCell.self, forCellWithReuseIdentifier: "editCell")
self.view.addSubview(editCollectionView)
editCollectionView.autoresizingMask = UIView.AutoresizingMask(rawValue: UIView.AutoresizingMask.RawValue(UInt8(UIView.AutoresizingMask.flexibleWidth.rawValue) | UInt8(UIView.AutoresizingMask.flexibleHeight.rawValue)))
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
editCollectionView.collectionViewLayout.invalidateLayout()
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
dismiss(animated: true)
let identifiers = results.compactMap(\.assetIdentifier)
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: identifiers, options: nil)
print("Identifiers: \(identifiers)")
print("FetchResults: \(fetchResult)")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photosInTheCellNow.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "editCell", for: indexPath) as! ImageEditCell
cell.imgView.image = photosInTheCellNow[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.frame.width
return CGSize(width: width/4 - 1, height: width/4 - 1)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
}
集合视图单元格
class ImageEditCell: UICollectionViewCell {
var imgView: UIImageView = {
var img = UIImageView()
img.contentMode = .scaleAspectFit
return img
}()
override func awakeFromNib() {
super.awakeFromNib()
}
override func prepareForReuse() {
super.prepareForReuse()
}
}