Obviously, ImageIO can do this. You need to add the io framework image to your project, and then this code works for me:
#import <ImageIO/ImageIO.h> // or @import ImageIO if modules enabled
#import <MobileCoreServices/MobileCoreServices.h>
+ (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;
}
source
share