我已经设法让OpenJPEG在我的iOS应用程序中编译,但我不知道从哪里开始尝试将UIImage转换为J2K文件或内存缓冲区中的J2K。建议?
答案 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;
}