Kingfisher 只下载并缓存图像一次,并在每个单元格中使用相同的图像,不会下载新图像。我试过使用 .ForceRefresh 选项,但它不起作用。
代码:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ImageCollectionViewCell
let imageURL = NSURL(string: "https://source.unsplash.com/random")
cell.imageView.kf_setImageWithURL(imageURL!)
return cell
}
最佳答案
在试用了 Kingfisher 之后,我得出的结论是问题根本不在于缓存。 .ForceRefresh
工作正常,即使使用单独的缓存位置也无法解决问题。
我认为问题在于 unsplash 处理对其 "https://source.unsplash.com/random"
端点的请求的方式。因为 Kingfisher 的所有请求几乎同时触发,所以 unsplash 会向您的所有请求发送相同的照片(可能是对它们的优化)。
您可以通过向
发出请求来获得更多随机性NSURL(string: "https://source.unsplash.com/random?page="+ String(indexPath.row))
(这可能是指即将发布的随机照片?),但即便如此,我还是注意到有些照片有时会出现两次。据我所知,这是最成功的方法。
幸运的是,至少通过这种方式您仍然可以利用 Kingfisher 的缓存。
关于ios - 如何使用 Kingfisher library swift 在每个单元格中下载和缓存新图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37218378/