2016-05-14 72 views
2

我正在使用Haneke庫進行下載,加載&緩存圖像。這個效果很好,除非滾動速度太快,它會加載不正確的圖像,或者根本沒有圖像。Swift - UITableView - 快速滾動時加載不正確的圖像

它的滾動速度比可以在後臺下載的速度快,因此無論隊列中的下一個圖像是否可以加載到不正確的單元格中。

以下是通過網絡從緩存請求圖像&的代碼。

let fetcher_net = NetworkFetcher<UIImage>(URL: finished_URL!) 
     let fetcher_disk = DiskFetcher<UIImage>(path: check_apost) 
     cache.fetch(fetcher: fetcher_disk).onSuccess { image in 
      //cell.card_imageIV.hnk_fetcher.cancelFetch() 
      //print("Image Cache found") 
      cell.card_imageIV.image = image 
      }.onFailure{ image in 
       //print("Unavailable to find image cache, fetching from network") 
       cache.fetch(fetcher: fetcher_net).onSuccess { image in 
        //print("Network image request SUCCESS") 
        cell.card_imageIV.image = image 
       } 
     } 

此外,在自定義單元格雨燕文件,有什麼我可以把下面的方法時,細胞是關閉屏幕,這將阻止任何要求嗎?

override func prepareForReuse() { 
    super.prepareForReuse() 
    // Increment the generation when the cell is recycled 

    //card_imageIV.hnk_cancelSetImage() 
    //card_imageIV.image = nil 
} 

我一直想弄清楚這幾個星期。如果有人有更好的庫來解決這個問題,請告訴我。

回答

1

我所做的就是像我這樣將圖像存儲在圖像緩存中。

 private var imageCache = [String:UIImage]() 

一旦我從任何地方提取過我的圖像,我將UIImage存儲在我的i​​mageCache數組中。

 self.imageCache["myImageFilename-01"] = img 
    self.imageCache["myImageFilename-02"] = img2 
    etc.... 

然後我將文件名存儲在我的單元格數據obj中。

//Basic structure example. 
class myData: NSObject 
    { 
    var imageFilename : String? 
    var titleText : Double? 
    etc... 
    } 

將數據存儲在obj中並將數組存儲在obj中。這將稍後用於獲取您的數據。

let newObj = myData() 
    newObj.imageFilename = "myImageFilename-01" 
    newObj.titleText = "some title text" 
    myDataArray.append(newObj) 

然後您可以像這樣設置圖像。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("TableCell", forIndexPath: indexPath) as! MyCustomCellClass 

    //I like to clear the image. Just incase. Not sure it is needed to do. 
    cell.myCellImage.image = nil //Or what ever you have wired up. 

    //Get the data from your array of myDataArray 
    let rowData = myDataArray[indexPath.row] 

    cell.myCellImage.image = self.imageCache[rowData.imageFilename] 

    //Set the rest of your cell stuff 

    return cell. 
    } 

這應該讓你朝着正確的方向前進。我認爲可能會有一些語法問題,我在沒有Xcode的計算機上編寫了這個問題。快樂編碼。

相關問題