5

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
pa12
  • 1,493
  • 4
  • 19
  • 40
  • iOS doesn't have a TIFF image library, as far as I know. You likely need to upload your image to the server, and let the server convert it to TIFF. – Matt H Jan 03 '12 at 15:49
  • Sadly no `-[NSImage TIFFRepresentation]` in iOS… – David Dunham Jan 18 '12 at 19:24
  • The errors mean that you have to add the libz library to your "Build Phases". Click on your project (upper right hand corner of XCode), click on your target, click "Build Phases", click "Link Binary with Libraries", click +, then choose libz.dylib. – Jeshua Lacock Feb 09 '12 at 00:01
  • Thanks That worked. I need some help in converting data types can you please look into the modified question. – pa12 Feb 09 '12 at 15:49

4 Answers4

11

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.

Jeshua Lacock
  • 5,730
  • 1
  • 28
  • 58
  • I tried couple of times. This is a c library and does not work when I include it to my ios project. I tried couple of ways but that does not work. – pa12 Feb 08 '12 at 15:52
  • There is no reason why C/C++ libraries can't be used in an iOS app. I use them all the time. What exactly have you tried, and why exactly does it not work? Please amend your question above. – Jeshua Lacock Feb 08 '12 at 16:34
  • You need to install the static library and header. You can download the libtiff static library from the image magick binary posted below. – Jeshua Lacock Feb 08 '12 at 19:29
  • I tried that. I added the libtiff.a lib file from imagemagick and added the tiff folder which has 4 header files. Should I specify the header search path also? – pa12 Feb 08 '12 at 19:59
  • I added the paths it works. I could not figure out how use the write.c and read.c into the project. I copied the code from write.c and put it a function it did not work. Can you please help with this. – pa12 Feb 08 '12 at 20:48
  • What exactly do you mean it does not work? I can't possibly help without much more detailed information. The comments aren't really intended for this, can you append your question? – Jeshua Lacock Feb 08 '12 at 21:04
  • Note too that you can't just import write as is, the function is declared as main(). You will have to at least rename the function. – Jeshua Lacock Feb 08 '12 at 21:13
  • I appened changes to the question. Please have a look at it. – pa12 Feb 08 '12 at 21:46
  • Hi din.. Were you able to convert images to TIFF? if so please share me the code – Vjlakshmi Feb 05 '13 at 07:00
  • Hi the link to the library seems broken, is there any updated link – Javal Nanda Apr 21 '14 at 04:40
7

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

Paul Cantrell
  • 71
  • 1
  • 1
  • 1
    Also you can specify writing options (compression, orientation etc) by passing dictionary instead of NULL. For more details you can check https://github.com/alexbutenko/TIFFWriter – Alex Sep 17 '14 at 04:38
  • This by far the best answer for those who want to avoid addign another library to their project. – Mike D Jul 08 '15 at 14:53
4

ImageMagick has been compiled for iOS and allows for handling of TIFF images. The instructions can be found here:

Link

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.

SETTING UP A PROJECT FOR IMAGEMAGICK

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:

Frameworks Group with Needed ImageMagick Frameworks

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:

Header Search Paths for ImageMagick

Now you should be able to include and use code from the ImageMagick framework.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
dtuckernet
  • 7,817
  • 5
  • 39
  • 54
0

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. :)

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81