Javier Flores Font
Javier Flores Font

Reputation: 35

iOS - Optimizing. Cache images

I'm trying to optimize an iOS application which contains a lot of images and code. I have reduced the size of images with some programs but using instruments reveals that the application is still taking among 70-90mb of cache memory. I have read that loading the resources(images) by demand and discard them when are not longer needed would be a good solution. How can i do it? I have also a question: When we use: UIImage *aux = [UIImage imagenamed:@"image.png"];
and after we write aux=nil; the image is discarded from cache?

Upvotes: 0

Views: 272

Answers (1)

Vern Jensen
Vern Jensen

Reputation: 3570

Are only some of those images visible at a time? Write a system that loads only those images currently visible (and perhaps some that you application thinks might become visible soon). When you get a memory warning from the system, look for some images you've loaded in the past that haven't been visible for a while and release them.

To answer your second question, yes, setting a reference to nil will release it IF you are using ARC (Automatic Reference Counting), and if the reference you set to nil is the only reference to that object. All references to an object must go away before it will be released.

I would look at some of the solutions available, such as Path's FastImageCache, and see if they meet your needs. FastImageCache stores uncompressed images on disk in a format similar to Sprite Sheets (used by 2D games) so they can be loaded quickly when needed. The emphasis here is on improving scrolling performance, so if that's not an issue for you, this might not be the right tool for the job.

You might also look at this thread, although this is aimed at caching web images.

You might also take a look at The Tumblr Image Cache

Upvotes: 0

Related Questions