Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory management when using UIImage

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

like image 620
lostInTransit Avatar asked Sep 21 '26 09:09

lostInTransit


1 Answers

[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.

like image 168
rustyshelf Avatar answered Sep 22 '26 21:09

rustyshelf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!