Luk*_*uke 13 uiimage catiledlayer ios cgimageref uiimagepngrepresentation
我正在生成一堆瓷砖CATiledLayer.在iPhone 4S上以4个级别的细节生成120个256 x 256的图块需要大约11秒钟.图像本身适合2048 x 2048.
我的瓶颈是UIImagePNGRepresentation.生成每256 x 256图像大约需要0.10-0.15秒.
我已经尝试在不同的背景队列上生成多个图块,但这只会将其减少到大约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);
}
Run Code Online (Sandbox Code Playgroud)
虽然这会产生较小的PNG文件(赢!),但它需要大约13秒,比以前多2秒.
有没有办法CGImage更快地编码PNG图像?也许使用NEON像libjpeg-turbo这样的ARM扩展(iPhone 3GS +)的库呢?
是否有比PNG更好的格式来保存不占用大量空间的瓷砖?
我能够提出的唯一可行选择是将磁贴大小增加到512 x 512.这会将编码时间减少一半.不知道那会对我的滚动视图做些什么.该应用适用于iPad 2+,仅支持iOS 6(使用iPhone 4S作为基线).
事实证明,性能如此差的原因是因为它每次都UIImageRepresentation解压缩原始图像,即使我认为我正在使用.CGImageCreateWithImageInRect
您可以在此处查看 Instruments 的结果:

注意_cg_jpeg_read_scanlines和decompress_onepass。
我用这个强制解压缩图像:
UIImage *image = [UIImage imageWithContentsOfFile:path];
UIGraphicsBeginImageContext(CGSizeMake(1, 1));
[image drawAtPoint:CGPointZero];
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
这个时间大约是0.10秒,几乎相当于每次UIImageRepresentation调用所花费的时间。
互联网上有许多文章推荐绘画作为强制解压图像的一种方式。
有一篇关于 Cocoanetics避免图像减压病的文章。文章提供了另一种加载图像的方法:
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);
Run Code Online (Sandbox Code Playgroud)
而现在同样的过程大约需要 3 秒!使用GCD并行生成图块可以更显着地减少时间。
上面的函数writeCGImage大约需要5秒。由于文件较小,我怀疑 zlib 压缩程度较高。
| 归档时间: |
|
| 查看次数: |
1941 次 |
| 最近记录: |