【问题标题】:Save CGImageRef as JPEG2000将 CGImageRef 保存为 JPEG2000
【发布时间】:2013-07-09 19:25:11
【问题描述】:

我正在尝试弄清楚如何将 CGImageRef 保存为 JPEG2000。

目前我得到了散乱的图像输出。

我有以下代码适用于除 kUTTypeJPEG2000 之外的所有其他格式。保存为 kUTTypeGIF 或 kUTTypePNG 可以正常工作。有趣的是,保存为不带 alpha 通道的 kUTTypeJPEG2000 也可以。

我怎样才能让图像正确输出?

- (void)saveImage:(CGImageRef)image path:(NSString *)path
{
    // Set the image compression quality
    CFMutableDictionaryRef properties = CFDictionaryCreateMutable(nil,
                                                                  0,
                                                                  &kCFTypeDictionaryKeyCallBacks,
                                                                  &kCFTypeDictionaryValueCallBacks);

    // Save and finalize
    CFURLRef url = CFBridgingRetain([[NSURL alloc] initFileURLWithPath:path]);

    CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypeJPEG2000, 1, NULL);
    CGImageDestinationAddImage(destination, image, properties);
    CGImageDestinationFinalize(destination);

    CFRelease(url);
}

【问题讨论】:

    标签: objective-c core-graphics


    【解决方案1】:

    固定。

    对于源图像,我们需要指定4 * width,而不是设置使用每行默认字节数创建上下文:CGImageGetBytesPerRow(image)

    size_t width = CGImageGetWidth(image);
    size_t height = CGImageGetHeight(image);
    size_t bytesPerRow = 4 * width;
    
    CGContextRef context = CGBitmapContextCreate(NULL,
                                                 width,
                                                 height,
                                                 CGImageGetBitsPerComponent(image),
                                                 bytesPerRow,
                                                 CGImageGetColorSpace(image),
                                                 CGImageGetAlphaInfo(image));
    

    【讨论】:

      猜你喜欢
      • 2010-11-22
      • 1970-01-01
      • 2015-04-30
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      相关资源
      最近更新 更多