Rich Byden
Rich Byden

Reputation: 519

Cutting down UIImage memory usage

I'm trying to find ways to cut down memory usage for my app right now, and I have an idea regarding UIImages. I make pretty extensive usage of UIImages, some of them common to multiple views. Is it a lot more memory efficient to instantiate a single UIImage and then use a pointer to that same image throughout the app rather than allocating a new UIImage for the same image in each view? Or, is the OS smart enough to sort of automatically cache a UIImage so that it is only stored in memory once? (doubt that's the case but I have to ask)

Thanks.

Upvotes: 1

Views: 1388

Answers (4)

Bruno Domingues
Bruno Domingues

Reputation: 1015

You use a lot of images, so remember that [UIImage imageNamed:@""]; caches the images, so if you are having low memory problem use instead [UIImage imageWithContentsOfFile:@""]; that doesn't cache it. Maybe that can help you cut down memory usage.

Upvotes: 1

ColdLogic
ColdLogic

Reputation: 7265

Straight from UIImage apple reference docs for imageNamed:

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

Upvotes: 3

zaph
zaph

Reputation: 112857

When a view unloads set the GUI properties to nil in the viewDidUnload method of UIViewController subclasses.

Upvotes: 0

Tim
Tim

Reputation: 14446

Yes, it is more efficient to store a single UIImage and use pointers to it.

It is unlikely that the OS caches images in this way, and even if it did, using pointers gives you more fine control of when the image is released / deallocated so that you don't have to hope the OS is going to take care of it.

Upvotes: -1

Related Questions