hermeneutic
hermeneutic

Reputation: 45

UIImage in TableView eating memory

i have tableView with 12 images. 1 for each row. The image size is 536 x x536 px (and 804 x 804 for @3x) all in JPG format with about 250kb file size. When i use Simulator, the memory reach 40MB for that tableView only. Yes, i've used dequeueReusableCellWithIdentifier. here is the screenshot :

enter image description here

is it normal? or how i can improve it? because i use UIImage(named:....) so it's cached. I haven't try to use real device, still waiting for my credit card.. :)

here my code for cellForRowAtIndexPath :

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let kolom = tableView.dequeueReusableCellWithIdentifier("portraitkolom", forIndexPath: indexPath) as portraitdetailTableViewCell
        let portrait = portraits[indexPath.row]

        kolom.portraitdetailgambar.image = UIImage(named: portrait.thumbImage)
        kolom.portraitdetailgambar.layer.shadowColor = UIColor(red: 200.0/255.0, green: 200.0/255.0, blue: 200.0/255.0, alpha: 1.0).CGColor
        kolom.portraitdetailgambar.layer.shadowOffset = CGSizeMake(0, 2)
        kolom.portraitdetailgambar.layer.shadowRadius = 0
        kolom.portraitdetailgambar.layer.shadowOpacity = 1.0

        return kolom
    }

UPDATE :

using UIImage(contentsOfFile:) seems a bit reducing memory usage. here is the screenshot :

enter image description here

updated code :

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let kolom = tableView.dequeueReusableCellWithIdentifier("portraitkolom", forIndexPath: indexPath) as portraitdetailTableViewCell
        let portrait = portraits[indexPath.row]
        let paths = NSBundle.mainBundle().pathForResource(portrait.thumbImage, ofType: "jpg", inDirectory: "portraitthumb")

        kolom.portraitdetailgambar.image = UIImage(contentsOfFile: paths!)
        kolom.portraitdetailgambar.layer.shadowColor = UIColor(red: 200.0/255.0, green: 200.0/255.0, blue: 200.0/255.0, alpha: 1.0).CGColor
        kolom.portraitdetailgambar.layer.shadowOffset = CGSizeMake(0, 2)
        kolom.portraitdetailgambar.layer.shadowRadius = 0
        kolom.portraitdetailgambar.layer.shadowOpacity = 1.0

        return kolom
    }

but it still 32MB.. is it normal? and it look like UIImage(contentsOfFile:) doesn't detect @2x or @3x images (case sensitive?).. so i rename my images to be someimage.jpg (previously [email protected]). Is that correct?

thanks.

Upvotes: 0

Views: 2141

Answers (2)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

some tips for you if u want image in your cell:

  1. Make sure the images that you want to put in your cell are the exact size of the space they'll be drawn into the cell. This means that the images are only as large as they need to be. This has two advantages: First you don't take up anymore space in your disk cache and in memory cache than you need to be. The in memory part is important because this means your app will feel snappier an look like it's loading less. Second, if the images are the exact size as they need to be drawn in then rendering them does not require expensive resizing code to run.
  2. Even though you seem to be doing this already I'll put this down here for clarity and posterity: Never, ever, load images from disk/web in tableView:cellForRowAtIndexPath:, always have all the images in memory before you hand it to a UIImageView in your cell. If you don't have the image at the moment, make an asynchronous request in a different thread to read off disk and then set the image once you have it in memory. One further optimization stemming from this technique, if you are being super picky and need it, is to only redraw these images when the table view has stopped scrolling. This visibly changes how things look, but might not be a bad thing if you're very image heavy, and for some reason you can't draw pixel to pixel on the screen.
  3. In terms of managing your in memory cache if you want to avoid low memory warnings (which isn't necessarily a bad thing if you diligently clean things up in your app when it happens), you might want to think about putting an artificial cap in your in memory cache. Say like 3x the number of images that fit on a screen, and those images that are in your cache are the first 3 consecutive screenfuls down (or up depending on your app) from where you are. Sorry for being vague here, but this part really depends on how your app is used.
  4. One last thing I can think of for your specific case is to flush the in memory cache for the table view that you tab away from. That way you are only holding images that are likely to be seen. Whether that makes sense, again, depends on the usage of your app.

reference from HERE.

Upvotes: 3

Stranger
Stranger

Reputation: 96

Just for performance improvement. do the following steps

  1. right now in your code images in list view is getting more memory than usual.
  2. must use imageWithContentsOfFile:path to get and display image in image view. I am sure it will help you a lot.

For details Info check this link: http://www.jorambarrez.be/blog/2012/04/19/ios-imagenamed-vs-imagewithcontentsoffile/

Upvotes: 0

Related Questions