While working on the CSCW Lab project, I encountered a situation in which the XML-RPC call returned an image, of course encoded as Base64. And guess what – in its known style, Apple doesn’t provide Base64 encoding and decoding – quite lame, given the fact that this encoding is used everywhere in data transfer over the internet – e-mail, browsers, web services – all of them use at some point this encoding to overcome the different local encodings on each one’s machine.
Once identified this problem, I had to solve it somehow – but guess what? – over the free sources there aren’t too many functions that provide this simple and basic encoding.
Finally, after few hours of searching, I finally found that Eric Czarny had a very successful implementation of this in its Cocoa XML-RPC Framework . After taking a quick look at its code, I end up using and importing into my project the
- NSStringAdditions.h. NSStringAdditions.m – providing the new category
+ (NSString *)base64StringFromData: (NSData *)data length: (int)length;
- NSDataAdditions.h and NSDataAdditions providing the new category:
+ (NSData *)base64DataFromString: (NSString *)string;
Being categories, they are automatically added to NSString and NSData automatically at runtime, thus their usage is straight forward :
UIImageView *uiIV; // currentElementValue holds the string representation of the image, encoded in Base64 NSData *nsD = [NSData base64DataFromString: [dataLayer currentElementValue]]; if ([nsD ){ uiIV = [[UIImageView alloc] initWithImage:[UIImage imageWithData: nsD]]; }