0
我使用Kingfisher緩存存儲在Firebase中的圖像。我正在通過對Firebase的URL調用來檢索圖像,然後嘗試緩存該圖像以供重新使用。 configureCell(with video:Video)
中的以下邏輯先前位於cellForItemAt
,圖像緩存工作得很好。但是,將此邏輯移至自定義單元並從cellForItemAt
調用此函數後,圖像不會通過緩存進行檢索。每個圖像都會下載,如果它重新出現在集合視圖中,則會重新下載。我的代碼如下。我在這裏先向您的幫助表示感謝。使用KingFisher自定義集合視圖單元緩存圖像
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
}