Any way to encode a PNG faster than UIImagePNGRepresentation?

走远了吗. 提交于 2019-12-03 05:45:23

It turns out the reason why UIImageRepresentation was performing so poorly was because it was decompressing the original image every time even though I thought I was creating a new image with CGImageCreateWithImageInRect.

You can see the results from Instruments here:

Notice _cg_jpeg_read_scanlines and decompress_onepass.

I was force-decompressing the image with this:

UIImage *image = [UIImage imageWithContentsOfFile:path];
UIGraphicsBeginImageContext(CGSizeMake(1, 1));
[image drawAtPoint:CGPointZero];
UIGraphicsEndImageContext();

The timing of this was about 0.10 seconds, almost equivalent to the time taken by each UIImageRepresentation call.

There are numerous articles over the internet that recommend drawing as a way of force decompressing an image.

There's an article on Cocoanetics Avoiding Image Decompression Sickness. The article provides an alternate way of loading the image:

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);

And now the same process takes about 3 seconds! Using GCD to generate tiles in parallel reduces the time more significantly.

The writeCGImage function above takes about 5 seconds. Since the file sizes are smaller, I suspect the zlib compression is at a higher level.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!