herbrandson
herbrandson

Reputation: 2417

Performance problems with [UIImage imageNamed] in iOS7/iOS8

I have an app that I'm in the process of updating to iOS8 from iOS6. After making a few small changes here and there to get things to compile, I ran the app on my iPad mini and found that the performance had dropped off significantly. In a couple places things that used to have no noticeable delay now frequently take 2-3 seconds to complete. Thinking that perhaps something was wrong with my iPad, I removed my current build and re-downloaded the app from the AppStore. Performance went back to the previous/good state.

After profiling the code, one of the things that sticks out is a significant amount of time in [UIImage imageNamed]. My app makes many calls to this because it has an indoor and an outdoor mode which requires most of the images on a given view to be loaded dynamically. Has something happened to this call in the latest SDKs?

When I profiled the app, here is the method that is eating up >70% of the time

+(UIImage*)imageNamed:(NSString*)rootName extension:(NSString*)extension
{
    NSString* typeName = _isIndoor ? @"inside" : @"outside";
    NSString* fullName = [NSString stringWithFormat:@"%@-%@.%@", rootName, typeName, extension];
    UIImage* result = [UIImage imageNamed:fullName];

    if (result == nil) {
        NSLog(@"Missing image [name: %@]", fullName);
    }

    return result;
}

Some other notes:

  1. The original app works fine on my 1st generation iPad Mini running iOS8
  2. The app does not currently use ARC, but I did test switching to ARC w/ no improvement (not surprising)
  3. The app does not use Asset Catalogs, but I tried converting to use Asset Catalogs w/ no improvement (also not surprising)
  4. I did some googling, but wasn't able to find anyone else having the same problem

Upvotes: 2

Views: 847

Answers (1)

KirkSpaziani
KirkSpaziani

Reputation: 1972

I assume the [UIImage imageNamed: ] is what is taking up the most time. Considering things were working great before we can make a guess that the "searching" part is what is going awry. Where are the resources stored? Main Bundle or elsewhere?

Also imageNamed: should cache the image, making subsequent calls fast. Is that the case? Or are subsequent calls for the same resource slow as well?

try using the init method on NSImage that takes the inBundle parameter, and pass the correct bundle into that. This way, if the search for the file is the problem, this should be faster.

Upvotes: 1

Related Questions