I am trying to create a TIFF
image from a UIImage
. I looked into Apple's docs but could not find any information.
Can anyone help me and explain how to create a TIFF
image on an iPad?
I am trying to create a TIFF
image from a UIImage
. I looked into Apple's docs but could not find any information.
Can anyone help me and explain how to create a TIFF
image on an iPad?
It seems to me that ImageMagick is way overkill just to write tiffs. Why not build libtiff? iOS is supported from it, and is what most software packages use to write tiffs (including ImageMagick).
You can even use the libtiff.a file from the ImageMagick link above. Just install the lib and tiff headers into your project. EDIT:
Here is a nice tutorial showing you how to write a tiff once you have installed libtiff. The second part of the tutorial shows you how to control the compression.
I feel like this answer is a little late, but in case someone else wants to know how to do this, I think you'll find the following will do the trick:
NSMutableData *mData = [NSMutableData dataWithCapacity:SOME_BIG_NUMBER];
CGImageDestinationRef myImageDest = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)(mData), kUTTypeTIFF, 1, NULL);
CGImageDestinationAddImage(myImageDest,image.CGImage, NULL);
CGImageDestinationFinalize(myImageDest);
CFRelease(myImageDest);
The mData will then hold NSData encoding of a TIFF file which you can then store to the disk, upload to a server, whatever...
You might need to include MobileCoreServices and ImageIO frameworks...
Paul
ImageMagick has been compiled for iOS and allows for handling of TIFF images. The instructions can be found here:
There is also a Sample Project that illustrates how to integrate this functionality into your application. It should be noted that including this library takes several steps - and the instructions are included in the link above.
First, download the latest version (from the first link above) with libs in the name. This includes pre-compiled versions of the needed frameworks. Unzip the contents of this file. Next, right-click Frameworks in your iOS projects and select the option to Add Files. Add each of the files with the .a extension. You should have something resembling this:
Next, you'll need to add the proper header search paths. Go to the Build Settings for your project and search for Header Search Paths. Select this and add a new search path that corresponds to where the include directory is (from the download). Don't add this just for debug or release, but for all. You should now have something that looks like this:
Now you should be able to include and use code from the ImageMagick framework.
Just use following code and you got the .tiff image
. You have to add ImageIO
and MobileCoreServices
Frameworks into your project.
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/UTCoreTypes.h>
- (void)viewDidLoad {
[super viewDidLoad];
[self convertImageIntoTIFF];
}
-(void)convertImageIntoTIFF{
UIImage *img = [UIImage imageNamed:@"image.png"];
float compression = 1.0; // Lossless compression if available.
int orientation = 1; // Origin is at top, left.
CFStringRef myKeys[3];
CFTypeRef myValues[3];
CFDictionaryRef myOptions = NULL;
myKeys[0] = kCGImagePropertyOrientation;
myValues[0] = CFNumberCreate(NULL, kCFNumberIntType, &orientation);
myKeys[1] = kCGImagePropertyHasAlpha;
myValues[1] = kCFBooleanTrue;
myKeys[2] = kCGImageDestinationLossyCompressionQuality;
myValues[2] = CFNumberCreate(NULL, kCFNumberFloatType, &compression);
myOptions = CFDictionaryCreate( NULL, (const void **)myKeys, (const void **)myValues, 3,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSLog(@"documentsDirectory %@", documentsDirectory);
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"image.tiff"];
[self writeCGImage:img.CGImage toURL:[NSURL fileURLWithPath:filePath] withType:kUTTypeTIFF andOptions:myOptions];
}
- (void) writeCGImage: (CGImageRef) image toURL: (NSURL*) url withType: (CFStringRef) imageType andOptions: (CFDictionaryRef) options
{
CGImageDestinationRef myImageDest = CGImageDestinationCreateWithURL((CFURLRef)url, imageType, 1, nil);
CGImageDestinationAddImage(myImageDest, image, options);
CGImageDestinationFinalize(myImageDest);
CFRelease(myImageDest);
}
For More details, you can able to download this DemoCode.
Hope, this is what you're looking for. :)