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?
How do I convert UIImage to J2K (JPEG2000) in iOS?
J2K? You want to save a Jpeg 2000 file? I've done that in Mac OS, but not in iOS. Poking around in the docs a little bit I think you'd have to create a CGImageDestination and give it a type of kUTTypeJPEG2000. –
Bozcaada
@DuncanC This worked amazingly well and resulted in a way smaller image than JPG with transparency preserved. Thanks so much for pointing me to that. –
Deserving
Apparently ImageIO can do this. You'll need to add image io framework to your project and then this code is working for me:
#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;
}
Last time I checked, iOS did not support
kUTTypeJPEG2000
. The type identifier constant was present but encoding did not work. Which version of iOS did you try? –
Unmannered This is working on iOS 5+. Haven't tested earlier iOS versions. –
Deserving
Looks like it's supported on iOS 3+ - extern const CFStringRef kUTTypeJPEG2000 __OSX_AVAILABLE_STARTING(__MAC_10_4,__IPHONE_3_0); –
Deserving
on iOS 7 you need import <MobileCoreServices/MobileCoreServices.h> in order to use kUTTypeJPEG2000 . –
Centrifugal
@PsychoDad do you know if converting to jpeg2000 as slow as it used to be? Or has the performance gotten better with newer devices/versions of iOS? –
Acyl
for line CGImageDestinationSetProperties(destinationRef, NULL); getting log that property is not dictionary. would it cause some issue. –
Byrd
Do I have to set destination property –
Byrd
© 2022 - 2024 — McMap. All rights reserved.