我正在尝试从库中选择多个图像,然后在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()
})
最佳答案
尝试这个,
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()
})
}
}
因为
asset.fetchOriginalImageWithCompleteBlock
是异步块。关于ios - 不使用DKImagePickerController重新加载CollectionView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40756983/