Jos*_*phH 5 prefetch uitableview uiimageview ios uicollectionview
我有一个我想象的非常常见的情况,我希望很容易解决 - 一个带有UICollectionView的应用程序,每个单元格中的图像通过http/https从Web服务器上的API获取.
我正在尝试在UICollectionView中实现iOS 10的预取,以便提前请求可能需要的图像.
在我看来,最佳预取应满足各种要求:
我查看了各种现有的库,如SDWebImage和Kingfisher,并且很惊讶,似乎没有任何方法可以轻松地使用任何一个库来满足上述要求.
例如,SDWebImage不符合第二个要求 - 您只能取消所有预取,或者必须为每个图像创建一个预取器(这意味着各种其他SDWebImage功能,例如限制预取图像的并发请求数) - 即要求5 - 不再有效.)
这真的是一个难题吗?我错过了一些明显的解决方案吗 我是否过度思考要求?
如果从prefetchItemsAt indexPaths传递的最新索引路径不够,您似乎需要实现自己的优先级。
要求 1、2、3 可以使用优先级队列(排序堆)来完成。要求 4 取决于您对优先级队列的实现。5 相当宽泛和模糊,只需限制对获取方法的调用次数即可。优先级队列采用通用类型,因此您可以将其与具有您想要获取的项目的属性的对象/类一起使用,为了简单起见,下面我仅使用 IndexPath.row。
创建一个优先级队列,充当数据源和缓存之间的中介。
然后通过查看/出队优先级队列并存储在 Kingfisher 中来设置要从数据源获取的数据。您的数据源 -> PriorityQueue -> Kingfisher -> Cell
func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
...
for indexPath in IndexPaths {
//Add the indexPath to the top of the priority queue
//highest priority is determined by the sort function you used when initializing the priority queue.
priorityQueue?.enqueue(indexpath.row)
}
//Call your method for getting the properties
self.fetchObjectsByPriority()
}
func fetchObjectsByPriority() {
if let count = priorityQueue?.count {
for _ in 0...count {
//get the index
var index = self.priorityQueue?.dequeue()
//Alternatively .peek() with out dequeuing item
//get image data and store
dataSource[index].image = ...
}
}
}
Run Code Online (Sandbox Code Playgroud)
确保使用排序函数初始化优先级队列。
您可以从这里获得预构建的优先级队列,您还需要一个堆来创建优先级队列。
如果没有相关代码,就很难提供有效的解决方案。我希望这有帮助,干杯,祝你好运!
| 归档时间: |
|
| 查看次数: |
1271 次 |
| 最近记录: |