2017-07-06 3 views
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 
} 

答えて

0

私は、キャッシュメカニズムが働いていたことを確認するために、次のコードを追加しました。 thumbnailImageView.kf.setImageが画像をキャッシュしていない理由を完全には分かっていませんが、以下のコードを追加することはそのトリックでした。

//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)], completionHandler: { (image, error, cacheType, url) in 
        ImageCache.default.store(image!, forKey: video.thumbnailurl!) 
        self.backgroundView = thumbnailImageView 
       }) 
      }) 
関連する問題