4

I have managed to get OpenJPEG to compile in my iOS app, but I have no clue where to begin trying to convert a UIImage to a J2K file or J2K in memory buffer. Suggestions?

4

1 回答 1

4

显然 ImageIO 可以做到这一点。您需要将图像 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;
}
于 2013-11-03T17:57:41.060 回答