0

我正在移交此方法并使用结果CGImageRef将其添加到CGImageDestinationRef设置为最终确定为的 a 中kUTTypeJPEG

- (CGImageRef)scaleImage:(CGImageRef)fullImageRef originalSize:(CGSize)originalSize scale:(CGFloat)scale;
    {
        CGSize imageSize = originalSize;

        CGRect scaledRect = CGRectMake(0.0, 0.0, imageSize.width * scale, imageSize.height * scale);
        CGColorSpaceRef colorSpace = CGImageGetColorSpace(fullImageRef);
        CGContextRef context = CGBitmapContextCreate(NULL, scaledRect.size.width, scaledRect.size.height, 8, 0, colorSpace, CGImageGetAlphaInfo(fullImageRef));
        CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
        CGImageRef subImage = CGImageCreateWithImageInRect(fullImageRef, scaledRect);
        CGContextDrawImage(context, scaledRect, subImage); // offscreen
        CGImageRef scaledImage = CGBitmapContextCreateImage(context);
        CGImageRelease(subImage);
        subImage = NULL;
        subImage = scaledImage;
        CGImageRelease(scaledImage);
        CGContextFlush(context);
        CGContextRelease(context);
        return subImage;
    }

我需要确保生成的 JPEG 是与原始图像最匹配的颜色。

我不清楚的是 CGImageAlphaInfo 常量对于我的目的的实际作用。

我读过 Quartz 文档,以及优秀的 Programming With Quartz 一书(由 Gelphman 和 Laden 撰写),但我仍然不确定我的方法。

我已经在 jpeg 的属性字典中将属性 kCGImageDestinationLossyCompressionQuality 设置为 1.0,同时捕获其他属性。

我应该做更多的事情来保持原件的颜色完整性吗?

这是macos,使用gc。

4

1 回答 1

1

没关系,因为 JPEG 不支持 alpha(至少,在没有外部遮罩extension 的情况下不支持),因此您可以预期 CGImageDestination 会在导出阶段丢弃 alpha 通道。

我会先尝试原始图像的 alpha 信息,如果我不能以这种方式创建位图上下文,我会使用kCGImageAlphaNoneSkipFirstor 或kCGImageAlphaNoneSkipLast. 当然,对于其他目标文件格式,我会使用 alpha-with-premultiplied-colors 像素格式之一。

此页面包含 CGBitmapContext 支持的组合的完整列表。

于 2010-12-19T01:14:49.017 回答