Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何异步加载UICollectionViewCell的图像而不存在重用问题?_Ios_Uicollectionview_Drawrect_Kingfisher - Fatal编程技术网

Ios 如何异步加载UICollectionViewCell的图像而不存在重用问题?

Ios 如何异步加载UICollectionViewCell的图像而不存在重用问题?,ios,uicollectionview,drawrect,kingfisher,Ios,Uicollectionview,Drawrect,Kingfisher,问题背景:我正在使用翠鸟为我的应用程序中的所有收藏视图和表格视图下载和设置图像。这很好,但我有一个特定的用例,我很确定翠鸟没有内置 我尝试做的是:为单个集合视图单元格下载多个图像,然后用这些图像更新单元格 为什么不使用.kf.setImage(…):集合视图单元格UI都是从draw(rect:CGRect) 我现在做的是:在我的UICollectionViewCell中,我有一个图像数组 var海报:[UIImage]=[]{ 迪塞特{ setNeedsDisplay() } } 我把他们拉进

问题背景:我正在使用翠鸟为我的应用程序中的所有收藏视图和表格视图下载和设置图像。这很好,但我有一个特定的用例,我很确定翠鸟没有内置

我尝试做的是:为单个集合视图单元格下载多个图像,然后用这些图像更新单元格

为什么不使用
.kf.setImage(…)
:集合视图单元格UI都是从
draw(rect:CGRect)

我现在做的是:在我的
UICollectionViewCell
中,我有一个图像数组

var海报:[UIImage]=[]{
迪塞特{
setNeedsDisplay()
}
}
我把他们拉进牢房

context.draw(图像,in:CGRect(x:0,y:-posterWindowFrame.minY,宽度:actualPosterWidth,高度:actualPosterHeight))
在我的视图控制器中,我将图像加载到
willDisplayCell:forItemAtIndexPath:
中,如下所示

override func collectionView(collectionView:UICollectionView,willDisplay单元格:UICollectionViewCell,forItemAt indepath:indepath){
让imageProviders=viewModel.imageProviders(位于:indexPath)
var images=[UIImage]()
let group=DispatchGroup()
让cachedIndexPath=indexPath
imageProviders.forEach{中的提供程序
guard let provider=provider else{return}
group.enter()
_=KingfisherManager.shared.retrieveImage(具有:。提供程序(提供程序),选项:[.targetCache(posterCache)]){结果为
DispatchQueue.main.async{
推迟{
小组请假()
}
切换结果{
成功案例(让图像显示结果):
images.append(imagesult.image)
案例.失败:
打印(“无可用图像”)
}
}
}
}
通知组(队列:.main){
警卫
将cell=collectionView.cellForItem(位于:cachedIndexPath)设为ListCollectionViewCell
else{return}
cell.posters=图像
}
}
这是可行的,但我可以看到图像的变化(这可能是通过预取来修复的),但我也注意到在滚动时有很多延迟,而我在注释这个函数时没有看到

除了获取
ViewDidLoad
中的所有图像(或其他任何地方,由于API速率限制),我如何加载这些图像,而不会造成延迟,也不会出现单元重用问题

编辑-8/13/19 我使用
uicollectionviewdatasourcerefetch
获取图像并在本地缓存它们,然后在
cellForRow
中设置图像,目前看来效果不错