gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:56:15 24 4
gpt4 key购买 nike

我正在处理一个显示图像的 tableView,它会在每次加载 View 时重新下载图像。有没有办法缓存图像以便它只下载一次图像? (缓存图片)

最佳答案

你可以这样做:

在你的 tableViewDelegate 中创建一个缓存对象

var myCache = NSCache()

然后在rowAtIndexPath中

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()
}
}

最后在单元格中

func configureCell(item:Item,image:UIImage?){
self.item = item


if image != nil{
//The image exist so you assign it to your UIImageView
img.image = image
}else{
//Create the request to download the image
}

}

关于ios - 有没有办法在用户每次打开应用程序时不重新下载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37603508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com