【发布时间】: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