gpt4 book ai didi

ios - CollectionView 不使用 DKImagePickerController 重新加载

转载 作者:行者123 更新时间:2023-11-29 00:35:14 28 4
gpt4 key购买 nike

我正在尝试从库中选择多张图片,并在 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是异步 block 。

关于ios - CollectionView 不使用 DKImagePickerController 重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40756983/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com