2016-05-15 252 views
2

我剛開始使用KingFisher。使用Kingfisher,UITableView中的圖像加載不正確

當你向UITableView滾動的速度過快時,所有網絡/ doadload請求都會得到備份,所以按時間停止滾動,它仍然會完成那些先前的請求。

這會導致所有圖像閃爍不同的圖片,直到所有備份的請求都完成爲止。

這是我的代碼從緩存或網絡檢索圖像。

if ImageCache.defaultCache.cachedImageExistsforURL(finished_URL!) == true { 
      print("image is already cached.") 

      if cell.tag == indexPath.row{ 
       cell.card_imageIV.image = UIImage(imageLiteral: "ic_error") 
       KingfisherManager.sharedManager.retrieveImageWithURL(finished_URL!, optionsInfo: nil, progressBlock: nil, completionHandler: { (image, error, cacheType, imageURL) ->() in 
        cell.card_imageIV.image = image 
        print("Retrieved cached image") 
       }) 
      } 

     }else{ 
      cell.card_imageIV.kf_setImageWithURL(finished_URL!, placeholderImage: self.placeholderImage, optionsInfo: nil, completionHandler: { image, error, cacheType, imageURL in 
       print("\(card_name): Finished") 
      }) 
     } 

我已經試過了取消所有以前的下載任務,使用但並沒有幫助下在https://github.com/onevcat/Kingfisher文檔。

下面的代碼我想:

// The image retrieving will stop. 
imageView.kf_cancelDownloadTask() 

這僅取消當前的細胞,至少從我可以做的意義。

回答

2

我和我的項目有完全相同的問題。我正在尋找一種方法來實現同樣的功能:如果我滾動得非常快, - 我想取消以前的下載。這裏是我的這個方法,用翠鳥庫(SWIFT 3):

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentify) 
    cell?.imageView?.kf.cancelDownloadTask() 

} 

此方法必須在表視圖委託聲明。它將取消已經滾動的單元格的下載任務任務。

希望這會有所幫助!

+0

嗨,感謝您的回答,我也有類似的競爭條件,取消didEndDisplaying我解決了這個問題。另外在Android流行的圖書館Glide使用類似的取消方法,唯一的缺點是如果它幾乎完全下載,它不會被緩存。你找到更好的解決方案嗎? – Minas

+1

不,我沒有辦法緩存半下載(或95%下載,如果這是你想要的方式)圖像。我不確定翠鳥/任何其他緩存庫支持這樣的事情。 –

+0

好吧,沒什麼大不了的,我使用你的解決方案,它工作得很好。非常感謝! – Minas

相關問題