What what
What what

Reputation: 527

Large amounts of memory allocated on setImage:

I have a UIImageView that was created programmatically ([[UIImageView alloc] init]). The app's memory stays in check until the setImage: method is called. Any thoughts?

Upvotes: 1

Views: 138

Answers (1)

rebello95
rebello95

Reputation: 8576

I'm assuming you're setting your image to your image view using something like this:

[imgV setImage:[UIImage imageNamed:@"yourImg.png"]]

The problem with using that is that the app caches these images. If you'd like to avoid caching images, use imageWithContentsOfFile::

[imgV setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"yourImg.png" ofType:nil]]];

Also, be sure to set your image to nil when you're done using it:

[imgV setImage:nil];

I have had issues with this in the past, and here's some text from an email I got back from Apple in response to a TSI:

There are quite a few cases where you’re using the API UIImage +imageNamed: to load images but you should be aware that imageNamed caches its image data even after the returned UIImage object is released. Replacing calls to imageNamed with -imageWithContentsOfFile: as outlined below is a way to ensure full control over your app’s image data in memory

Upvotes: 3

Related Questions