问题描述
我正在尝试使用PHPickerViewController从选择器中获取照片并将其添加到收藏夹视图中。由于PHPickerViewController太新了,因此我发现的信息非常有限。我以为我必须将其转换为PHAsset,但是这样做没有运气。下面的代码将正确运行,但是集合视图显示为空白。我知道我缺少一些在选择器委托中处理结果的代码,这是因为我尝试的每个解决方案都失败了。我想我还需要处理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,minimumLinespacingForSectionAt section: Int) -> CGFloat {
return 1.0
}
func collectionView(_ collectionView: UICollectionView,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()
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)