Avoid overusing [UIImage imageNamed]

April 18, 2009 § 1 Comment

[UIImage imageNamed] can bring down your application, and/or Springboard after confirming a few sources.  [UIImage imageNamed] run out of memory when it tries to cache too many images.

At least iPhone OS 2.0 and 2.1 use an internal cache for images loaded from disk using imageNamed:. With low memory, this cache is not clear up completely. [UIImage imageNamed] is considered a static method.

Sometimes you may not even get any leaks, any out of bound memory accesses, memory usage. The app, even the Springboard will terminate without warning. 3.0 MB image cached images is enough to trigger the memory overload from [UIImage imageNamed].

Those system memory warnings will appear on Debugging Console showing background processes, Springboard termination, Application termination.

Possible caching approach is to declare NSMutableDictionary *thumbnailCache

- (UIImage*)thumbnailImage:(NSString*)fileName
{
UIImage *thumbnail = [thumbnailCache objectForKey:fileName];

if (nil == thumbnail)
{
NSString *thumbnailFile = [NSString stringWithFormat:@"%@/thumbnails/%@.jpg", [[NSBundle mainBundle] resourcePath], fileName];
thumbnail = [UIImage imageWithContentsOfFile:thumbnailFile];
[thumbnailCache setObject:thumbnail forKey:fileName];
}
return thumbnail;
}

Do not use [UIImage imageNamed] for any significant amount of images.

Advertisements

§ One Response to Avoid overusing [UIImage imageNamed]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Avoid overusing [UIImage imageNamed] at Web Builders.

meta

%d bloggers like this: