Re: Base64 encoding of NSImage
Re: Base64 encoding of NSImage
- Subject: Re: Base64 encoding of NSImage
- From: Andreas Grosam <email@hidden>
- Date: Mon, 02 Jul 2012 14:08:41 +0200
On 01.07.2012, at 17:23, Alexander Hartner wrote:
> I am looking at available option to send an NSImage to a webserver. I came across gSoap however its licensing model makes it not suitable. Using REST / JSON seems like an easy option however NSDATA does not seem to be supported by the JSON Serialiser. The one remaining option I considered was to encode the image myself to base64 before including in the JSON request. This could work quite nicely, however there doesn't seem to be a standard method for base64 encoding.
Well, embedding an image encoded in base64 in a JSON document is not that "RESTful" anyway. You better provide a link (URI) in the JSON to the image resource and load the image data separately (still being RESTful) but using a different transport format which is capable to transmit binary data more efficiently.
Anyway, if you want to embed the image data into JSON:
The various JSON libraries define a "mapping" of Foundation types to JSON types. For instance, a NSDictionary maps to a JSON Object, a NSNumber maps to a JSON Number or JSON Boolean (depending on the underlaying type in NSNumber), and so force.
Usually, a NSImage will not be mapped directly to a JSON type, and especially with NSJSONSerialization you cannot accomplish this. If you try to serialize a hierarchy of Foundation objects which contains an object for which there is no mapping defined, the serialization fails for obvious reasons.
There are third party JSON libraries which are capable to customize the mapping, though. For instance, it might suffice to implement a method for a Category for NSImage to accomplish this (JPJson library uses this approach for example). That way, you can serialize a hierarchy of Foundation objects which contains a NSImage - no matter where this occurs in the tree. But note, a NSImage can only be mapped to one of the existing JSON primitive types, which is a JSON String. This in turn requires the mapping method to encode binary data to Unicode, for example base64.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden