问题描述
我试图找出如何获取标识符数组并将其以编程方式放入集合视图中的方法。如果我错了,请纠正我,但是,我相信最好的方法是使用PHAsset.fetchAssets
。我最多希望用户获取10张图像(平均3-4张),因此我认为该过程不会占用大量资源。我正在用特定的标识符数组测试下面的代码。在分配资产的行的cellForItemAt函数中,我收到一条错误消息。请参阅该行上方的注释。我在SO上看到过类似的问题,但是我无法应用发现的任何内容。我在做什么错了?
var identifier: [String] = ["83BDF9C0-E1FB-4F68-9F2C-99649A67E218/L0/001","18D08CEC-4075-4488-BFD1-E9403B214356/L0/001","74257416-6D9C-48B2-9CE0-C23B56CB4171/L0/001","2EA7CACE-9B9B-4FC9-ABC5-03BCE2BDAA2A/L0/001","46A4CD2B-4FEB-4BBE-86DC-B79ED49BC300/L0/001","E4290EB0-2B16-46AE-9B98-83379F6128F3/L0/001","7C0B36FF-320D-4083-92E4-5A1F464FFFB1/L0/001","DEE1414C-2B92-4147-8C3C-A1775FB324AD/L0/001"]
class DetailViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
var myCollectionView: UICollectionView!
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
var fetchResult: PHFetchResult<PHAsset>!
override func viewDidLoad() {
requestOptions.isSynchronous = false
requestOptions.deliveryMode = .opportunistic
let layout = UICollectionViewFlowLayout()
myCollectionView = UICollectionView(frame: self.view.frame,collectionViewLayout: layout)
myCollectionView.delegate = self
myCollectionView.dataSource = self
myCollectionView.backgroundColor = UIColor.white
myCollectionView.allowsMultipleSelection = true
myCollectionView.register(ImageEditCell.self,forCellWithReuseIdentifier: "Cell")
myCollectionView.showsVerticalScrollIndicator = false
self.view.addSubview(myCollectionView)
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell",for: indexPath) as! ImageEditCell
cell.representedAssetIdentifier = identifier[0]
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = false
// LINE BELOW PRODUCES ERROR: Cannot convert value of type '[String]' to expected element type 'Array<String>.ArrayLiteralElement' (aka 'String')
let asset = PHAsset.fetchAssets(withLocalIdentifiers: [identifier],options: .none).firstObject
imgManager.requestImage(for: asset!,targetSize: CGSize(width:120,height: 120),contentMode: .aspectFill,options: requestOptions,resultHandler: { result,info in
if cell.representedAssetIdentifier == self.identifier {
cell.imageview.image = result
}
})
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)