我正在为CATiledLayer生成一堆磁贴。在iPhone 4S上以256 x 256和4个细节级别生成120个平铺大约需要11秒。图像本身适合2048 x 2048。
我的瓶颈是UIImagePNGRepresentation。每生成一张256 x 256的图像大约需要0.10-0.15秒。
我尝试过在不同的背景队列上生成多个tiles,但这只将时间缩短到了9-10秒。
我还尝试通过如下代码使用ImageIO框架:
- (void)writeCGImage:(CGImageRef)image toURL:(NSURL*)url andOptions:(CFDictionaryRef) options
{
CGImageDestinationRef myImageDest = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, (__bridge CFStringRef)@"public.png", 1, nil);
CGImageDestinationAddImage(myImageDest, image, options);
CGImageDestinationFinalize(myImageDest);
CFRelease(myImageDest);
}虽然这会生成较小的PNG文件(win!),但它需要大约13秒,比以前多了2秒。
有没有办法更快地从CGImage中对PNG图像进行编码?也许是一个像libjpeg-turbo一样使用NEON ARM扩展(iPhone 3GS+)的库?
在不占用大量空间的情况下,是否有比PNG更好的格式来保存磁贴?
我能想到的唯一可行的选择是将平铺大小增加到512x512。这将编码时间缩短了一半。我不确定这会对我的滚动视图造成什么影响。该应用程序用于iPad 2+,仅支持iOS 6(以iPhone 4S为基准)。
发布于 2013-01-02 11:41:54
事实证明,UIImageRepresentation表现如此之差的原因是,尽管我认为我是在用CGImageCreateWithImageInRect创建一个新图像,但它每次都会解压缩原始图像。
您可以在此处查看Instruments的结果:

请注意_cg_jpeg_read_scanlines和decompress_onepass。
我用下面的代码对图像进行了强制解压缩:
UIImage *image = [UIImage imageWithContentsOfFile:path];
UIGraphicsBeginImageContext(CGSizeMake(1, 1));
[image drawAtPoint:CGPointZero];
UIGraphicsEndImageContext();此操作的时间大约为0.10秒,几乎相当于每次UIImageRepresentation调用所用的时间。
在互联网上有许多文章推荐将绘画作为一种强制解压缩图像的方式。
有一篇关于Cocoanetics Avoiding Image Decompression Sickness的文章。本文提供了另一种加载图像的方法:
NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:(id)kCGImageSourceShouldCache];
CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)[[NSURL alloc] initFileURLWithPath:path], NULL);
CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, 0, (__bridge CFDictionaryRef)dict);
UIImage *image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
CFRelease(source);现在,同样的过程大约需要3秒!使用GCD并行生成tiles可以更显著地减少时间。
上面的writeCGImage函数大约需要5秒。由于文件较小,我怀疑zlib压缩级别较高。
https://stackoverflow.com/questions/14116194
复制相似问题