gpt4 book ai didi

ios - 在 Apple 的 Core Graphics/ImageIO 框架中的 CGImageDestination 中设置 JPEG 压缩质量时没有效果

转载 作者:行者123 更新时间:2023-11-29 10:36:27 24 4
gpt4 key购买 nike

我正在编写 IOS 应用程序,我需要将 CGImage 保存为 JPEG 文件。控制压缩质量很重要。

我已经编写了下面提供的函数。它有效,因为我得到了一个 JPEG 文件。但无论我为压缩设置什么,我总是得到相同的结果。即文件大小始终相同。

谁能告诉我我做错了什么?

void CGImageWriteJPEG(CGImageRef image, NSString *path) {
NSMutableData * data = [[NSMutableData alloc] init];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)data, kUTTypeJPEG, 1, NULL);

float compression = .8; // What I put here does not seem to matter...

CFStringRef myKeys[1];
CFTypeRef myValues[1];
CFDictionaryRef myOptions = NULL;
myKeys[0] = kCGImageDestinationLossyCompressionQuality;
myValues[0] = CFNumberCreate(NULL, kCFNumberFloatType, &compression);
myOptions = CFDictionaryCreate( NULL, (const void **)myKeys, (const void **)myValues, 1,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);


CGImageDestinationSetProperties(destination, myOptions);
CGImageDestinationAddImage(destination, image, nil);
CGImageDestinationFinalize(destination);



[data writeToURL:[NSURL fileURLWithPath:path] atomically:NO];

CFRelease(destination);
CFRelease(myOptions);
CFRelease(myValues[0]);
}

最佳答案

尝试将 myOptions 传递给 CGImageDestinationAddImage() 而不是

CGImageDestinationAddImage(destination, image, myOptions);

这是我必须在 swift 中做的事情,所以我认为它是一样的。例如

let imageProperties = [kCGImageDestinationLossyCompressionQuality as String: 0.8]
CGImageDestinationAddImage(destination, image, imageProperties)

swift 3 的更新答案

let imageProperties = [kCGImageDestinationLossyCompressionQuality as String: 0.8] as CFDictionary
CGImageDestinationAddImage(destination, image, imageProperties)

关于ios - 在 Apple 的 Core Graphics/ImageIO 框架中的 CGImageDestination 中设置 JPEG 压缩质量时没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26742540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com