我正在使用 Haneke 库来下载、加载和缓存图像。这很好用,除非滚动太快,它会加载不正确的图像,或者有时根本没有图像。
它的滚动速度比在后台下载的速度要快,因此无论队列中的下一个图像是什么,都可以加载到错误的单元格中。
这是通过网络和缓存请求图像的代码。
let fetcher_net = NetworkFetcher<UIImage>(URL: finished_URL!)
let fetcher_disk = DiskFetcher<UIImage>(path: check_apost)
cache.fetch(fetcher: fetcher_disk).onSuccess { image in
//cell.card_imageIV.hnk_fetcher.cancelFetch()
//print("Image Cache found")
cell.card_imageIV.image = image
}.onFailure{ image in
//print("Unavailable to find image cache, fetching from network")
cache.fetch(fetcher: fetcher_net).onSuccess { image in
//print("Network image request SUCCESS")
cell.card_imageIV.image = image
}
}
此外,在自定义单元格 Swift 文件中,我可以在以下方法中放入任何内容,以在单元格离开屏幕时停止任何请求?
override func prepareForReuse() {
super.prepareForReuse()
// Increment the generation when the cell is recycled
//card_imageIV.hnk_cancelSetImage()
//card_imageIV.image = nil
}
几个星期以来,我一直试图弄清楚这一点。如果有人有更好的库来解决这个问题,请告诉我。