[英]Photos in tableView from local gallery uses too much RAM
When I try to get photos using a URL from a local gallery I add them to a tableView using ALAssetsLibrary()
当我尝试使用本地画廊中的URL获取照片时,我使用ALAssetsLibrary()
将其添加到tableView中
My code: 我的代码:
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
}
I try to minimize the size of photos, it helps a little but it doesn't solve the problem if I need to use many photos (10 or more...) 我尝试将照片的尺寸最小化,但有一定帮助,但是如果我需要使用很多照片(10张或更多),则无法解决问题。
What is self.cachedImages
. 什么是self.cachedImages
。 If that's a dictionary then your code is holding ALL of your images in memory, and as you scroll your memory footprint will grow and grow. 如果那是一本字典,那么您的代码会将所有图像保存在内存中,并且随着滚动,您的内存占用量将越来越大。 The simplest way to fix that would be to get rid of the cache dictionary entirely and load the images from the asset library every time, but that might be slow. 最简单的解决方法是完全摆脱缓存字典,每次都从资产库加载图像,但这可能很慢。
If you are scaling your images to display them in a table view then load the asset, open a graphics context at the target size, render the image at your thumbnail size, and cache the thumbnail image. 如果要缩放图像以在表格视图中显示它们,然后加载资源,以目标尺寸打开图形上下文,以缩略图尺寸渲染图像,然后缓存缩略图。
You might also look at using NSCache
, which acts like a dictionary but manages flushing objects when memory gets low. 您可能还会看到使用NSCache
,它的作用类似于字典,但是当内存不足时管理刷新对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.