私は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
}