如何在iOS中将UIImage转换为J2K(JPEG2000)?

时间:2013-11-03 03:14:33

标签: ios jpeg2000

我已经设法让OpenJPEG在我的iOS应用程序中编译,但我不知道从哪里开始尝试将UIImage转换为J2K文件或内存缓冲区中的J2K。建议?

1 个答案:

答案 0 :(得分:4)

显然ImageIO可以做到这一点。您需要将image io框架添加到项目中,然后此代码对我有用:

#import <ImageIO/ImageIO.h> // or @import ImageIO if modules enabled
#import <MobileCoreServices/MobileCoreServices.h>

// ...    

// quality is 0-1 (0 = smallest file size, 1 = lossless quality)
+ (NSData*) convertToJPEG2000:(UIImage*)image withQuality:(float)quality
{
    NSMutableData* d = [NSMutableData data];
    CGImageDestinationRef destinationRef = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)d, kUTTypeJPEG2000, 1, NULL);
    CGImageDestinationSetProperties(destinationRef, NULL);
    CGImageDestinationAddImage(destinationRef, image.CGImage, (__bridge CFDictionaryRef)@{ (NSString*)kCGImageDestinationLossyCompressionQuality: @(quality) });

    if (!CGImageDestinationFinalize(destinationRef))
    {
        d = nil;
    }
    CFRelease(destinationRef);

    return d;
}