使用KingFisher使用自定义集合视图单元格发布缓存图像

时间:2017-07-06 02:00:37

标签: swift caching uicollectionview kingfisher

我使用Kingfisher缓存存储在Firebase中的图片。我正在使用对Firebase的网址调用来检索图像,然后尝试缓存该图像以供重复使用。 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
}

1 个答案:

答案 0 :(得分:0)

我添加了以下代码以确保缓存机制有效。不完全确定为什么 <div> <i class="fa fa-comment fa-fw"></i> <img src="{{url('images/account/'.$notifications->user->currentimage}}" height="50px" width="50"/> {{$notifications->user->email}} commented on your post. <span class="pull-right text-muted small">{{$notifications->created_at->diffForHumans()}}</span> 没有缓存图像,但添加下面的代码就可以了。

thumbnailImageView.kf.setImage