tags:

views:

718

answers:

3

I need to create a base64 string representation of an NSImage cocoa object. What's the best way of handling this, apple documentation seems to be a little short on the subject (or I just cant find it). Base64 encoding seems rather complex from the outside.

Any help would be very much appreciated.

Cheers Alex

EDIT

NSArray *keys = [NSArray arrayWithObject:@"NSImageCompressionFactor"];
NSArray *objects = [NSArray arrayWithObject:@"1.0"];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSImage *image = [[NSImage alloc] initWithContentsOfFile:[imageField stringValue]];
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithData:[image TIFFRepresentation]];
NSData *tiff_data = [imageRep representationUsingType:NSPNGFileType properties:dictionary];

NSString *base64 = [tiff_data encodeBase64WithNewlines:NO];
+1  A: 

Apple doesn't provide any particular help here, so you do have to tackle the complexity on your own, one way or another.

Fortunately, there are some resources available to make this easier. The first approach is to literally do the encoding and decoding yourself. Google Toolbox for Mac has a good example of this approach and you might be able to just use this source file as-is:

http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/GTMBase64.m

If you're building only for the Mac, where the OpenSSH libraries are available, then you could take advantage of some functions in those libraries to do the encoding and decoding:

http://www.dribin.org/dave/blog/archives/2006/03/12/base64_cocoa/

danielpunkass
+1  A: 

I've got a bunch of code, including Base64 parsing based on the implementation in OmniFoundation, in my toolkit on github. In particular, look at Extensions/NSData+Base64.h.

Jim Dovey
+1  A: 

An NSImage is a very abstract object. NSImage doesn't really care whether it's a raster image or a vector image; an NSImage object can even have raster, vector, and even programamtic representations all at once—it's that general.

Before you can generate Base64 data, you must decide what you want to encode.

The first step is to decide whether you want to encode a raster or vectors. The former is quite easy, and I'd guess it's probably what you meant. However, an NSImage could have come from a vector source, such as a PDF document.

If you know you created the image from a raster source, you can just encode that source data.

If it came from a vector source, you can still just encode that if you know the application on the decoding end will be able to handle it (e.g., if it's another Cocoa or Cocoa Touch app). On the other hand, if the app on the decoding end may be unable to handle vector data, then you should avoid this tactic.

The one solution that works in all cases is to use NSBitmapImageRep to create a raster capture of the image. Lock focus on the image, then create an NSBitmapImageRep using that method, then unlock focus. Then, use representationUsingType:properties: to generate PNG (or whatever format is appropriate) data for the image. Then Base64-encode the PNG (or whatever format) data.

Peter Hosey
I'm guessing that Alex doesn't want to base64 encode the image for his own purposes, but to meet the contract of some recipient, e.g. a web service. That being the case, he'll need to base64 exactly what the web service expects (probably JPG or PNG data).I doubt there is much need for speculation on the variety of ways to encode an image, but perhaps Alex will confirm one way or the other.
danielpunkass
Hey Guys, I think I have the base64 encoding working correctly using the code from dribin.org but while wordpress accepts and creates the file, its not an image. http://alexmillsdesign.files.wordpress.com/2009/06/quickicon5.jpg I believe that I'm not getting the right data from the NSImage object so I changed my code which is in the edit of my original question. I'm new to image programming, where am I going wrong?
Alex Mills
1. Use named constants (NSImageCompressionFactor, not @"NSImageCompressionFactor"). 2. Use an NSNumber instance, not a string containing a decimal representation. 3. Not that it matters, because PNG doesn't support NSImageCompressionFactor anyway. 4. You're creating and uploading PNG data, not JPEG. Either use .png as the filename extension, or create and upload JPEG data, not PNG. 5. If you download that, you'll find that it's base64 data. Maybe you don't actually need to base64-encode it?
Peter Hosey
6. You named your variable “tiff_data”, yet you put PNG data in it. Name your variables truthfully; it will save your sanity when you come back to read this code in a few months or years.
Peter Hosey
He's probably using WordPress's XMLRPC interface to upload the file. Since it's a text-based XML format, it explains why he needs the base64 encoding.
danielpunkass
danielpunkass: That doesn't explain why the contents of the file are base-64-encoded, unless he's base64ing it twice.
Peter Hosey