iphoneuiimageios5imagenamed

UIImage:imageWithContentsOfFile is 10 times slower in iOS 5.0


After simply recompiling our iPhone application on newly released iOS 5.0 SDK i faced strange problem - all UIImage:imageNamed (first call with actual image loading) and UIImage:imageWithContentsOfFile started to work 10 times slower than before. i managed to narrow problem down: this is the case only for jpeg and png files (not gifs!) and this is not because of file size. even straightforward loading of small 32*32 png takes around 300ms... compared to 30ms on older devices (checked on 3.1 and 4.3.5 with the exact same code)

i also tried to load image via newly introduced CIImage with this code

WLLog(@"Data loading...");
NSData *imageData = [NSData dataWithContentsOfFile:path];
WLLog(@"CIImage creation...");
CIImage* cii = [CIImage imageWithData:imageData];
WLLog(@"CIImage creation ok...");
float scle = 1.0;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    scle = [[UIScreen mainScreen] scale];
}
#endif
CIContext *context = [CIContext contextWithOptions:nil];
UIImage* res5 = [[UIImage alloc] init];
WLLog(@"UIImage creation...");
[res5 initWithCGImage:[context createCGImage:cii fromRect:cii.extent] scale:scle orientation:UIImageOrientationUp];
WLLog(@"Done!");

without any luck... this single line

CIImage* cii = [CIImage imageWithData:imageData];

takes the same 300ms even on small images (4Kb png). imho, there is simple nothing to parse at all!

Is there anything to resolve such strange change in loading times? For now it looks like something changed drastically in sdk internals :(


Solution

  • I had the same problem and it took me a few hours to find out what went wrong. Our two situations seemed to be exactly the same: An old project that didn't run very well on iOS5 any more.

    So I took Instrument's Time Profiler and dug into the depth of my app only to find out that every time the app hung it actually was in the process of opening PNG files for UIImageViews, just like you found out as well. But other apps I wrote don't have this problem, and I did everything the same way. So judging by what you experienced and that my other apps were running fine, I figured it must have something to do with the PNG files themselves. And guess what, it turned out that I was right.

    So I sat down and wrote a script that piped all PNG files through ImageMagick's convert to make TGAs out of them, then deleted the PNGs (just for good measure) and then converted the temporary TGAs back to PNG files. That way I made sure that they were not only NOT created by Photoshop any more, but also completely rewritten.

    That did the trick. Everything runs smoothly now, just as it did on iOS 3 and 4.

    I'm not sure if it had something to do with Photoshop. Other apps I recently did work fine with PNGs made with Photoshop. So maybe it was the version of Photoshop I used pretty much exactly a year ago to create those PNGs in the first place.

    Or maybe simply overwriting the old image files were enough, I'm not sure. But now it runs just fine.

    I hope this helps!