HurkNburkS HurkNburkS - 1 year ago 150
iOS Question

how to decode BASE64 encoded PNG using Objective C

I have some NSData which is BASE64 encoded and I would like to decode it, I have seen an example on stack over flow that looks like this

NSData* myPNGData = [xmlString dataUsingEncoding:NSUTF8StringEncoding];

[Base64 initialize];
NSData *data = [Base64 decode:img];
cell.image.image = [UIImage imageWithData:myPNGData];

However this gives me a load of errors, I would like to know what to do in order to get this to work. Is there some type of file I need to import into my project or do I have to include a framework?

These are the errors I get

Use of undeclared identifier 'Base64'
Use of undeclared identifier 'Base64'
Use of undeclared identifier 'cell'

I have looked everywhere and cannot figure out what is the proper thing to do.

any help would be greatly appreciated.

Answer Source

NSData Base64 library files will help you.

#import "NSData+Base64.h"

//Creating the data from your base64String
NSData *data = [[NSData alloc] initWithData:[NSData

//Now data is decoded. You can convert them to UIImage
UIImage *image = [UIImage imageWithData:data];

Hope it helps

Swift 3 Version

It's pretty much the same

//Create your NSData object
var data = NSData(base64Encoded: "yourStringData", options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)

//And then just create a new image based on the data object
let image = UIImage(data: data as! Data)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download