2016-05-14 71 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的计算机上编写了这个问题。快乐编码。

相关问题