gpt4 book ai didi

ios - 本 map 库中的 tableView 中的照片使用过多 RAM

转载 作者:行者123 更新时间:2023-11-30 13:20:16 24 4
gpt4 key购买 nike

当我尝试使用本 map 库中的 URL 获取照片时,我使用 ALAssetsLibrary() 将它们添加到 tableView 中

我的代码:

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

var cell: AllPhotosTableViewCell!
cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! AllPhotosTableViewCell

let url = array[indexPath.section]["photos"] as! [AnyObject]

if let image = cachedImages[url[indexPath.row] as! String] {
cell.imageViewImg.image = imageResize(image, sizeChange: CGSize(width: 50, height: 50))
} else {

let nsURL = NSURL(string: url[indexPath.row] as! String)!
var loadError: NSError?

asset.assetForURL(nsURL, resultBlock: { (asset) -> Void in

if let ast = asset {
let image = UIImage(CGImage: ast.defaultRepresentation().fullResolutionImage().takeUnretainedValue())
self.cachedImages[url[indexPath.row] as! String] = image

dispatch_async(dispatch_get_main_queue(), {

cell.imageViewImg.image = self.imageResize(image, sizeChange: CGSize(width: 50, height: 50))

})
}
}, failureBlock: {(error) -> Void in
loadError = error
})
}

return cell
}

我尝试最小化照片的大小,这有一点帮助,但如果我需要使用很多照片(10 或更多...),它并不能解决问题

最佳答案

什么是self.cachedImages。如果那是一本字典,那么您的代码会将所有图像保存在内存中,并且当您滚动时,您的内存占用量将会不断增加。解决这个问题的最简单方法是完全摆脱缓存字典并每次从资源库加载图像,但这可能会很慢。

如果您要缩放图像以在表格 View 中显示它们,则加载资源,以目标尺寸打开图形上下文,以缩略图尺寸渲染图像,然后缓存缩略图图像。

您还可以考虑使用 NSCache,它的作用类似于字典,但在内存不足时管理刷新对象。

关于ios - 本 map 库中的 tableView 中的照片使用过多 RAM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37839028/

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