UIImage:imageWithContentsOfFile 在 iOS 5.0 中慢了 10 倍
在新发布的 iOS 5.0 SDK 上简单地重新编译我们的 iPhone 应用程序后,我遇到了奇怪的问题 - 所有 UIImage:imageNamed (第一次调用实际图像加载)和 UIImage:imageWithContentsOfFile 开始工作比以前慢 10 倍。我设法缩小问题范围:这种情况仅适用于 jpeg 和 png 文件(不是 gif!),而且这不是因为文件大小。即使直接加载小型 32*32 png 也需要大约 300 毫秒...相比之下,旧设备上需要 30 毫秒(在 3.1 和 4.3.5 上使用完全相同的代码进行检查)
我还尝试使用此代码通过新引入的 CIImage 加载图像,
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!");
而无需任何操作运气好...即使在小图像(4Kb png)上,这一行
CIImage* cii = [CIImage imageWithData:imageData];
也需要同样的 300 毫秒。恕我直言,根本没有什么可解析的!
有什么办法可以解决加载时间的这种奇怪变化吗?目前看来 sdk 内部发生了巨大的变化:(
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 :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,我花了几个小时才找出问题所在。我们的两种情况似乎完全一样:一个旧项目,在 iOS5 上运行得不太好。
因此,我使用 Instrument 的 Time Profiler 深入研究了我的应用程序,结果发现每次应用程序挂起时,它实际上都是在为 UIImageViews 打开 PNG 文件的过程中,就像您也发现的那样。
但我编写的其他应用程序没有这个问题,我也以同样的方式完成了所有操作。因此,根据您的经历以及我的其他应用程序运行良好来判断,我认为这一定与 PNG 文件本身有关。你猜怎么着,事实证明我是对的。
所以我坐下来编写了一个脚本,通过 ImageMagick 的转换将所有 PNG 文件传输到其中,以生成 TGA,然后删除 PNG(只是为了更好的措施),然后将临时 TGA 转换回 PNG 文件。这样我就确保它们不仅不再由 Photoshop 创建,而且还被完全重写。
这就成功了。现在一切都运行顺利,就像在 iOS 3 和 4 上一样。
我不确定这是否与 Photoshop 有关。我最近做的其他应用程序可以很好地处理用 Photoshop 制作的 PNG。所以也许这是我一年前用来创建这些 PNG 的 Photoshop 版本。
或者也许简单地覆盖旧图像文件就足够了,我不确定。但现在它运行得很好。
我希望这有帮助!
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!
这很可能是一个错误。通过错误报告者向 Apple 提交雷达。请务必整理一个简单的项目来清楚地演示该错误并将其附加到错误报告中 - 否则苹果会向您发送一封电子邮件要求您提供该项目。
在此处发布您的雷达#,以便其他有类似问题的人在向 Apple 提交类似错误时也可以参考该#。
It could very well be a bug. Submit a radar to Apple via the bug reporter. Be sure to throw together a simple project which clearly demonstrates the bug and attach it to the bug report -- otherwise Apple will send you an email asking for one.
Post your radar # here so others with a similar issue can reference that # when submitting a similar bug to Apple as well.