iOS内存警告 [英] iOS Memory Warnings

查看:139
本文介绍了iOS内存警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用从Parse数据库下载的图像填充集合视图,但是我收到内存警告,偶尔会发生崩溃。有谁知道其他应用程序如何设置呈现如此多的图像而不会崩溃?有人能告诉我如何优化我已有的东西吗?以下是所有相关代码: https://gist.github.com/sungjp/99ae82dca625f0d73730

I'm trying to populate a collection view with images downloaded from a Parse database, but I'm receiving memory warnings followed by occasional crashes. Does anyone know how other apps manage to present so many images without crashing? Can someone show me how to optimize what I already have? Here's all the relevant code: https://gist.github.com/sungjp/99ae82dca625f0d73730

var imageCache : NSCache = NSCache()


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    self.imageCache.removeAllObjects()
}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    //In storyboard, my collection view cell has the identifier "PostCell"    
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PostCell", forIndexPath: indexPath) as! CollectionViewCell

    //I have an array called "posts" that's filled with PFObjects that have UIImage data
    let postings: PFObject = posts[indexPath.row]

    //setting my cell's string property called "objectId" to the PFObject's ID for the purpose of caching shown below
    cell.objectId = postings.objectId! as String

    let theImage: AnyObject = postings["imageFile"] as! PFFile

    if let image = self.imageCache.objectForKey(postings.objectId!) as? UIImage {

    cell.imageView.image = image
    println("had this photo in cache")

    } else {

        theImage.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in

        if error != nil {

            println("error caching or downloading image")
            return
        }

        let cellImage = UIImage(data:imageData!, scale: 1.0)
        self.imageCache.setObject(cellImage!, forKey: postings.objectId!)
        cell.imageView.image = cellImage
        println("just added another photo to cache")
        }
    }

    return cell
}


class CollectionViewCell: UICollectionViewCell {

    var objectId: String!

}


推荐答案


  1. 正如@Adrian B所说:阅读有关仪器的信息。

  2. 您可以考虑在保存之前缩小图像,或者在表格视图中保存一个额外的较小副本以供显示。这取决于你需要多么好的图像 - 大概是桌面视图不如具有MB数据的全尺寸图像那么大。实际上,如果图像被正确缩放,图像也会看起来更好。这本身就应该处理延迟。

  3. 您有onw缓存策略,但如果要提高性能,请尝试SDWebImage(请参阅: https://github.com/rs/SDWebImage )。该库缓存图像并异步下载它们。

  1. As @Adrian B said: read about Instruments.
  2. You might consider scaling down the images before you save them, or save an additional smaller copy for display in the table view. It depends how good images you need - presumably for the table view not as large as full scale pictures with MB of data. Actually, the images will also look better if they are properly scaled. This by itself should take care of the delays.
  3. You have your onw cache strategy, but if you want to improve performance, try SDWebImage (see: https://github.com/rs/SDWebImage). This library caches images and downloads them asynchronously.

这篇关于iOS内存警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆