ios - 如何使用 Kingfisher library swift 在每个单元格中下载和缓存新图像?

标签 ios swift ios9

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/

相关文章:

ios - 使用 Storyboard 屏蔽 UIView 并赋予圆角?

ios - UIView(子类)试图呈现 UIImagePickerController

ios - 在 ScrollView -Swift 项目中随时触摸隐藏键盘

ios - Segue 第一次推送 Nothing(发送数据延迟)

ionic-framework - JSON.stringify 无法序列化循环结构 : iOs9 error for Ionic/AngularFire app

css - iphone 粘性菜单 jquery onscroll ios 9

ios - dyld : Library not loaded: @rpath/Alamofire. 设备 iOS 9 上的框架/Alamofire 错误,无法编译

ios - UITableView 自定义单元格 - 如何将 cornerRadius 保持在编辑模式

swift - 链接器命令失败,使用 Flurry 退出代码 1

ios - 如何快速打开多个套接字连接?