views:

1840

answers:

1

Hi

My application heavily uses the UIImage object to change images on a single UIImageView based on user input and the previous image which was set. I use [UIImage imageNamed:] to create my UIImage objects.

I guess this is not the best way to use UIImage coz my memory usage keeps increasing with time and never drops. (Got to know this when I ran the app with Object Allocations and moreover there are no other NSString variables I am using, only BOOL and UIImage)

How should I effectively use UIImage and UIImageView objects to keep the memory low?

Thanks

+7  A: 
[UIImage imageNamed:]

caches the image you're loading into memory. This is great if you want to reuse the same set of images over and over again, but if you are constantly showing different (or large) images, then you should use:

NSString *fileLocation = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];
NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];

[UIImage imageWithData:imageData];

instead.

rustyshelf
Thanks rusty. I am using the same set of images. But the number of times this method is used in the code is to the tune of 200 or so. In case there is caching of images, why should the memory usage keep increasing over time? There are no leaks.
lostInTransit
Simply because leaks reports no leaks doesn't mean that there are no leaks ;)More importantly I can't really tell unless you post your code.
rustyshelf
I am facing the same issue while using images, I want to know what code need to be used if we are getting the images from sqlite database.
rkb