每次用户打开应用程序时,有没有办法不重新下载图像?

时间:2016-06-02 23:05:04

标签: ios swift firebase firebase-storage

我正在使用一个显示图像的tableView,每次加载视图时它都会重新下载图像。有没有办法缓存图像,所以它只下载一次图像? (缓存图像)

1 个答案:

答案 0 :(得分:1)

你可以这样做:

在tableViewDelegate中创建一个Cache对象

In [39]: df_stacked.unstack(level=0)
Out[39]:
            brazil  chile
Date
2014-09-19     2.7    NaN
2014-09-22     2.2    NaN
2014-10-30     NaN    1.3
2014-11-05     NaN    1.2

然后在rowAtIndexPath

var myCache = NSCache()

最后在牢房里

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if let row = tableView.dequeueReusableCellWithIdentifier("MyCell") as? MyCell{


        //Get the item
        let item = items[indexPath.row]

        //You need to cancel previous requests and reset the image to avoid render errors caused by the reusable row
        //here you should also cancel any previous requests made to avoid downloading an image that the user won't see because he scrolled 
        cell.img.image = nil

        //Search for the image in the cache
        var img:UIImage?
        img = myCache.objectForKey(item.image_url) as? UIImage

        row.configureCell( item: item,img:img)

        return row

    }else{
        return MyCell()
    }
}