我正在使用 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
}