本文介绍了使用ImageIO在iOS上创建渐进式jpeg在设备上生成块状结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 UIImage 对象创建一个渐进的jpeg,这是我的代码

I'm trying to create a progressive jpeg from a UIImage object, this is the code i'm

NSMutableData *data = [NSMutableData data];

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: @"Library/Caches/test.jpg"];

CFURLRef url = CFURLCreateWithString(NULL, (CFStringRef)[NSString stringWithFormat:@"file://%@", path], NULL);
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypeJPEG, 1, NULL);
CFRelease(url);

NSDictionary *jfifProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                (__bridge id)kCFBooleanTrue, kCGImagePropertyJFIFIsProgressive,
                                nil];

NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSNumber numberWithFloat:.7], kCGImageDestinationLossyCompressionQuality,
                            jfifProperties, kCGImagePropertyJFIFDictionary,
                            nil];

CGImageDestinationAddImage(destination, ((UIImage*)object).CGImage, (__bridge CFDictionaryRef)properties);
CGImageDestinationFinalize(destination);
CFRelease(destination);

这在运行在模拟器时工作得很好,但不幸的是在设备上产生chunky / blocky结果:

This works great when running in simulator, but unfortunately produces chunky/blocky results on device:

有什么想法吗?我会回到使用 UIImageJPEGRepresentation 作为最后的手段,我真的需要渐进的JPEG。

Any ideas on what's going on? i'd revert to using UIImageJPEGRepresentation as a last resort, I really need progressive JPEGs.

推荐答案

好消息:我刚刚在iPhone 4上测试过,并且图片看起来很棒:

Good news: I just tested this on an iPhone 4 and the image looks great:

NSDictionary *jfifProperties = [NSDictionary dictionaryWithObjectsAndKeys:
@72, kCGImagePropertyJFIFXDensity,
@72, kCGImagePropertyJFIFYDensity,
@1, kCGImagePropertyJFIFDensityUnit,
nil];

(使用新的文字语法)。

(using new literal syntax).

这些密度选项的含义是什么?。

A good JFIF reference for what these density options mean.

这篇关于使用ImageIO在iOS上创建渐进式jpeg在设备上生成块状结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 06:47