saving jpeg, tiff and png images in cocoa

February 22, 2009

The documentation about saving images from CGImages in cocoa is pretty vague to say the least. Here are some code snippets for saving jpeg, tiff and png files.

I’ve recently made a small cocoa app that renders high resolution images from sketchinz drawings. It was surprisingly easy to reuse most of the cocoa touch code from my iPhone app, but there were some stumbling blocks. One of them was saving images. The documentation pretty much sucks in this section. I found most of the information in the ImageApp example that comes  with the dev kit and sifting through header files.

 
-(void)saveJPEGImage:(CGImageRef)imageRef path:(NSString *)path {
	CFMutableDictionaryRef mSaveMetaAndOpts = CFDictionaryCreateMutable(nil, 0,
											&kCFTypeDictionaryKeyCallBacks,  &kCFTypeDictionaryValueCallBacks);
	CFDictionarySetValue(mSaveMetaAndOpts, kCGImageDestinationLossyCompressionQuality, 
						 [NSNumber numberWithFloat:1.0]);	// set the compression quality here
	NSURL *outURL = [[NSURL alloc] initFileURLWithPath:path];
	CGImageDestinationRef dr = CGImageDestinationCreateWithURL ((CFURLRef)outURL, (CFStringRef)@"public.jpeg" , 1, NULL);
	CGImageDestinationAddImage(dr, imageRef, mSaveMetaAndOpts);
	CGImageDestinationFinalize(dr);
}
 
 
-(void)savePNGImage:(CGImageRef)imageRef path:(NSString *)path {
	NSURL *outURL = [[NSURL alloc] initFileURLWithPath:path]; 
	CGImageDestinationRef dr = CGImageDestinationCreateWithURL ((CFURLRef)outURL, (CFStringRef)@"public.png" , 1, NULL);
	CGImageDestinationAddImage(dr, imageRef, NULL);
	CGImageDestinationFinalize(dr);
}
 
-(void)saveTIFFImage:(CGImageRef)imageRef path:(NSString *)path {
	int compression = NSTIFFCompressionLZW;  // non-lossy LZW compression
	CFMutableDictionaryRef mSaveMetaAndOpts = CFDictionaryCreateMutable(nil, 0,
																		&kCFTypeDictionaryKeyCallBacks,  &kCFTypeDictionaryValueCallBacks);
	CFMutableDictionaryRef tiffProfsMut = CFDictionaryCreateMutable(nil, 0,
																	&kCFTypeDictionaryKeyCallBacks,  &kCFTypeDictionaryValueCallBacks);
	CFDictionarySetValue(tiffProfsMut, kCGImagePropertyTIFFCompression, CFNumberCreate(NULL, kCFNumberIntType, &compression));	
	CFDictionarySetValue(mSaveMetaAndOpts, kCGImagePropertyTIFFDictionary, tiffProfsMut);
 
	NSURL *outURL = [[NSURL alloc] initFileURLWithPath:path];
	CGImageDestinationRef dr = CGImageDestinationCreateWithURL ((CFURLRef)outURL, (CFStringRef)@"public.tiff" , 1, NULL);
	CGImageDestinationAddImage(dr, imageRef, mSaveMetaAndOpts);
	CGImageDestinationFinalize(dr);
}

4 Responses to “saving jpeg, tiff and png images in cocoa”

  1. Did you manage to make it work on the iPhone? Apparently Image IO is not available on the device, and I ca’t find CGImagedestinationref.

    Looking

  2. Does switching to any format results in data loss? Which one do you recommend is best format for saving high resolution images?
    And please answer the above guy’s question, I wanna know that too.

    Thanks in advance!

  3. @sid: There is a loss of quality when saving jpeg images, but not with png and tiff. You can use any of theses image format to save high res images.

    This code is for osx only. It doesn’t work on iphone.

  4. Very nice! I searched through a few google search results for CGImageDestinationCreateWithURL and thought you have got to be kidding me it can’t be that hard.

    Very easy, very simple does exactly what you expect it to do.

    Thanks.

Leave a Reply