【问题标题】:Issue caching images with custom collection view cell using KingFisher使用 KingFisher 使用自定义集合视图单元缓存图像问题
【发布时间】:2017-07-07 02:48:56
【问题描述】:

我正在使用 Kingfisher 缓存存储在 Firebase 中的图像。我正在通过对 Firebase 的 url 调用检索图像,然后尝试缓存该图像以供重复使用。 configureCell(with video:Video) 中的以下逻辑以前在 cellForItemAt 中,并且图像缓存工作得很好。但是,在将此逻辑移动到自定义单元格并从cellForItemAt 调用此函数后,图像不会通过缓存检索。每个图像都会被下载,如果它重新出现在集合视图中,则会重新下载。我的代码如下。提前感谢您的帮助。

class ProminentVideoCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!

func configureCell(with video:Video) {
    self.timeLabel.text = video.date?.getElapsedInterval()
    let thumbnailImageView = UIImageView()
    ImageCache.default.retrieveImage(forKey: video.thumbnailurl!, options: nil) {
        image, cacheType in
        if let image = image {
            //Video thumbnail exists in cache--set it
            thumbnailImageView.image = image
            self.backgroundView = thumbnailImageView
            print("Get image \(image), cacheType: \(cacheType).")

        } else {
            //Video thumbnail does NOT exist in cache, download it
            video.downloadThumbnailFromStorage(with: { (url) in
                let resource = ImageResource(downloadURL: url, cacheKey: video.thumbnailurl!)
                let processor = BlurImageProcessor(blurRadius: 40.0) >> RoundCornerImageProcessor(cornerRadius: 20)

                thumbnailImageView.kf.setImage(with: resource, options: [.processor(processor)])
                self.backgroundView = thumbnailImageView
            })


        }
    }
}

在我的视图控制器中:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let currentVideo = self.selectedVideosArray.object(at: indexPath.row) as! Video
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ProminentVideoCollectionViewCell
        cell.configureCell(with: currentVideo)
        return cell
}

【问题讨论】:

    标签: swift caching uicollectionview kingfisher


    【解决方案1】:

    我添加了以下代码以确保缓存机制正常工作。不完全确定为什么 thumbnailImageView.kf.setImage 没有缓存图像,但添加下面的代码就可以了。

    //Video thumbnail does NOT exist in cache, download it
                video.downloadThumbnailFromStorage(with: { (url) in
                    let resource = ImageResource(downloadURL: url, cacheKey: video.thumbnailurl!)
                    let processor = BlurImageProcessor(blurRadius: 40.0) >> RoundCornerImageProcessor(cornerRadius: 20)
    
                    thumbnailImageView.kf.setImage(with: resource, options: [.processor(processor)], completionHandler: { (image, error, cacheType, url) in
                        ImageCache.default.store(image!, forKey: video.thumbnailurl!)
                        self.backgroundView = thumbnailImageView
                    })
                })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 2017-10-18
      • 2017-10-11
      • 1970-01-01
      相关资源
      最近更新 更多