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 *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];
Do not use [UIImage imageNamed] for any significant amount of images.