NSData to Base64 NSString & Base64 NSString to NSData – Conversion/Encoding

This post is surely going to help for easy conversion between Base64 NSString & NSData. I will guide you how to encode & decode.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// This will load NeonSpark image & *data* variable is holding data of image NSData *data = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"Neon Spark" ofType:@"png"] ]; // using base64StringFromData method, we are able to convert data to string NSString *str = [NSString base64StringFromData:data length:[data length]]; // log the base64 encoded string NSLog(@"Base64 Encoded string is %@",str); // using base64DataFromString NSData *dataFromBase64=[NSData base64DataFromString:str]; // log the decoded NSData length NSLog(@"Data length is %i",[dataFromBase64 length]); |
You can always use methods base64DataFromString and base64StringFromData if you have properly imported following file. NSStringAdditions.h … Continue reading