ios - 具有许多图像的 UITableView 内存使用率高

标签 ios iphone uitableview memory uiimage

您好,我正在使用 MKMapSnapshotter 生成 map 图像并使用 SDWebImage 缓存它们。 map 图像将显示在每个 uitableview 单元格中。

我遇到的问题是大约 30 个 uitableview 单元格使用的内存是 130 MB,如果我不使用 map 图像,则使用的内存是 25 MB,最后使用 map 图像但没有缓存(如生成每次显示一个单元格时映射图像)使用的内存为 50 MB。

如何减少内存使用量?或者我如何存储图像以减少它们占用的内存空间?任何帮助,将不胜感激。 我的代码如下。

在类(class)的顶端:

var imageCache: SDImageCache!
var mySnapOptions: MKMapSnapshotOptions!
let regionRadius: CLLocationDistance = 300
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)

在 viewDidLoad() 中:

imageCache = SDImageCache(namespace: "myNamespace")
mySnapOptions = MKMapSnapshotOptions()
mySnapOptions.scale = UIScreen.mainScreen().scale

在 cellForRow 中:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> TableViewCell? {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell

    if let post = object {
        cell.mapImageView.image = UIImage(named: "MapPlaceholder")

        let tempLoc = post["location"] as! PFGeoPoint
        let loc = CLLocation(latitude: tempLoc.latitude, longitude: tempLoc.longitude)

        imageCache.queryDiskCacheForKey(post.objectId, done: {(image: UIImage?, cacheType: SDImageCacheType!) in
            if let cachedImage = image {
                cell.mapImageView.image = cachedImage
            }else {
                cell.mapPinImageView.hidden = true
                cell.mapActivityIndicator.startAnimating()

                dispatch_async(self.queue) { () -> Void in
                    self.mySnapOptions.region = MKCoordinateRegionMakeWithDistance(loc.coordinate,
                        self.regionRadius * 2.0, self.regionRadius * 2.0)
                    self.mySnapOptions.size = (cell.mapImageView.image?.size)!

                    MKMapSnapshotter(options: self.mySnapOptions).startWithCompletionHandler { snapshot, error in
                        guard let snapshot = snapshot else {
                            print("Snapshot error: \(error)")
                            return
                        }
                        self.imageCache.storeImage(snapshot.image, forKey: post.objectId, toDisk: true)
                        dispatch_async(dispatch_get_main_queue(), {
                            cell.mapActivityIndicator.stopAnimating()
                            cell.mapImageView.image = snapshot.image
                            cell.mapPinImageView.hidden = false
                        })
                    }

                    //
                }
            }
        })


    }
    return cell
}

最佳答案

我最终使用了不同的缓存框架。 FastImageCache,使我能够以出色的滚动性能和低内存使用率实现预期的结果,因为图像不是存储在内存中而是存储在磁盘上(这就是 FastImageCache 的工作原理)。

关于ios - 具有许多图像的 UITableView 内存使用率高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35281433/

相关文章:

ios - 如何快速获取本地和子网掩码IP地址

iphone - 如何在 iphone 中使用 openCV 检测眨眼

iphone - iOS开发中如何处理自定义事件?

ios - UITableViewCell 编辑附件颜色

objective-c - 如何在 iOS 上使用 ARC 实现自定义复制属性

ios - 通过什么工具制作此ios类UML图?

ios - 从 iPhone gmail 应用程序打开所有链接打开我的应用程序

iphone - XML特殊字符解析

objective-c - 在哪里放置 drawrect 在 cellforrowatindexpath 中?

ios - 在另一个 View Controller 展开 segue 后重新加载 UIView 数据上的自定义 TableView