简体   繁体   English

来自本地画廊的tableView中的照片使用了过多的RAM

[英]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.

相关问题 TableView滞后太多,占用了太多内存Swift - TableView lagging too much and taking up too much memory Swift 导航控制器堆栈使用过多资源 - navigation controller stack uses too much resources camera.getPicture在选择画廊的cordova ios中落后了太多时间 - camera.getPicture is lagging too much of time in cordova ios from select gallery LZMA SDK使用太多RAM解压缩iOS(xcode) - LZMA SDK decompress for iOS (xcode) using too much RAM 从pdf数据中提取单个页面(或页面范围)而不加载整个pdf(有时需要太多RAM) - Extract a single page (or range of pages) from pdf data without loading the whole pdf (which takes too much RAM sometimes) 使用Photos框架从图库上传视频 - Upload videos from gallery using Photos framework 使用UIImagepickercontroller从Iphone照片库获取照片 - Get photos from Iphone photo gallery with UIImagepickercontroller 如何从iPhone的图片库中删除选定的照片? - How to delete selected photos from the Gallery in iPhone? 使用 xctest 从照片/图库中选择图像 - pick image from photos/gallery using xctest Flutter:拍摄照片 - 保存到图库 - 从图库中读取照片 - Flutter: Capture photo - Save to Gallery - Read photos from Gallery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM