【发布时间】:2017-04-07 00:30:21
【问题描述】:
我正在尝试从库中挑选多张图片,并在UICollectionView 上查看。选择图像时它工作正常,但图像没有出现在UICollectionView 上。我尝试再次选择图像,并查看以前的图像。当前选择的不是。
代码如下:
imagePicker.didSelectAssets = {[unowned self] (assets: [DKAsset]) in
print("Selected!")
for asset in assets {
asset.fetchOriginalImageWithCompleteBlock({ (image, info) in
guard let imageData = UIImageJPEGRepresentation(image!, 0) else {
print("There is no image bro..!")
return
}
let thumbnails = UIImage(data: imageData)
self.imageArray.append(thumbnails!)
print(image!)
})
}
self.collectionView.reloadData()
}
这里是cellForItemAt:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "selectedImage", for: indexPath) as! SelectedImageCell
cell.image = imageArray[indexPath.item]
return cell
}
对于任何想知道我如何解决问题的人。这是代码。
let group = DispatchGroup()
for asset in assets {
group.enter()
asset.fetchOriginalImageWithCompleteBlock({ (image, info) in
guard let imageData = UIImageJPEGRepresentation(image!, 0) else {
print("There is no image bro..!")
return
}
let thumbnails = UIImage(data: imageData)
self.imageArray.append(thumbnails!)
print(image!)
group.leave()
})
}
group.notify(queue: .main, execute: {
print("finish..")
self.collectionView.reloadData()
})
【问题讨论】:
-
您在 for 循环过程完成后重新加载您的集合后尝试。
-
请出示您的
cellForItemAt indexPath方法。 -
@YogendraGirase 已经尝试过了。
-
您可以尝试像这样重新加载吗
DispatchQueue.main.async {self.collectionView.reloadData()} -
并在您的打印语句下方编写重新加载方法
print(image!)
标签: ios image uicollectionview