Erik Nolander
Erik Nolander

Reputation: 65

Memory management when adding a resized UIImage to a UITableViewCell

I have images cached and I want to resize them and display them in a tableview. Here's the code:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://huntify.com%@",[[hunts objectAtIndex:indexPath.row] iconURL]]];
UIImage *logo = [[UIImage alloc] initWithContentsOfFile:[[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url]];
logo = [logo imageScaledToSize:CGSizeMake(100.0, 100.0)];    
[[cell imageView] setImage:logo];

This works fine but I wonder how I should release the UIImage. I suspect that releasing the "logo" after setting the image-property to the UIImageView is wrong because of the imageScaledToSize-method. Should I assign a new UIImage when resizing and release the old one?

Upvotes: 0

Views: 444

Answers (3)

Damien
Damien

Reputation: 2421

Remember, you are only releasing owenership of an object when you [logo release], you are not dealloc'ing it.

Upvotes: 0

joerick
joerick

Reputation: 16448

Do this-

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://huntify.com%@",[[hunts objectAtIndex:indexPath.row] iconURL]]];
UIImage *logo = [[UIImage alloc] initWithContentsOfFile:[[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url]];
UIImage *resizedLogo = [logo imageScaledToSize:CGSizeMake(100.0, 100.0)];    
[[cell imageView] setImage:resizedLogo];
[logo release];

Upvotes: 1

Ole Begemann
Ole Begemann

Reputation: 135548

You have a big memory leak here. By assigning another value to logo in logo = [logo imageScaledToSize:CGSizeMake(100.0, 100.0)];, you lose the reference to the original image you would need to release. So yes, you should use a new variable to store the result of imageScaledToSize: and then release the original logo.

Upvotes: 1

Related Questions