我刚开始使用 KingFisher。
当您向下滚动 UITableView 的速度过快时,所有网络/加载请求都会得到备份,因此当它停止滚动时,它仍在完成之前的请求。
这会导致所有图像以不同的图片闪烁,直到所有备份请求完成。
这是我从缓存或网络中检索图像的代码。
if ImageCache.defaultCache.cachedImageExistsforURL(finished_URL!) == true {
print("image is already cached.")
if cell.tag == indexPath.row{
cell.card_imageIV.image = UIImage(imageLiteral: "ic_error")
KingfisherManager.sharedManager.retrieveImageWithURL(finished_URL!, optionsInfo: nil, progressBlock: nil, completionHandler: { (image, error, cacheType, imageURL) -> () in
cell.card_imageIV.image = image
print("Retrieved cached image")
})
}
}else{
cell.card_imageIV.kf_setImageWithURL(finished_URL!, placeholderImage: self.placeholderImage, optionsInfo: nil, completionHandler: { image, error, cacheType, imageURL in
print("\(card_name): Finished")
})
}
我已经尝试按照 https://github.com/onevcat/Kingfisher 上的文档进行操作取消所有以前的下载任务,使用但没有帮助。
我试过以下代码:
// The image retrieving will stop.
imageView.kf.cancelDownloadTask()
这只会取消当前的单元格,至少从我的理解来看是这样。
最佳答案
我的项目有完全相同的问题。我正在寻找一种方法来实现与您想要的相同的东西:如果我滚动得非常快,我想取消以前的下载。这是我使用 KingFisher 库(Swift 3)的方法:
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentify)
cell?.imageView?.kf.cancelDownloadTask()
}
此方法必须在 TableView 委托(delegate)中声明。它将取消已滚动单元格的下载任务。
希望对您有所帮助!
关于swift - 使用 Kingfisher,图像无法在 UITableView 中正确加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37240590/