5

I have some NSData which is Base-64 encoded and I would like to decode it, I have seen an example 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.

jscs
  • 63,694
  • 13
  • 151
  • 195
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

6 Answers6

12

You can decode a Base64 encoded string to NSData:

-(NSData *)dataFromBase64EncodedString:(NSString *)string{
    if (string.length > 0) {

        //the iPhone has base 64 decoding built in but not obviously. The trick is to
        //create a data url that's base 64 encoded and ask an NSData to load it.
        NSString *data64URLString = [NSString stringWithFormat:@"data:;base64,%@", string];
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:data64URLString]];
        return data;
    }
    return nil;
}

Example use of above method to get an image from the Base64 string:

-(void)imageFromBase64EncodedString{

    NSString *string = @"";  // replace with encocded string
    NSData *imageData = [self dataFromBase64EncodedString:string];
    UIImage *myImage = [UIImage imageWithData:imageData];

    // do something with image
}
bobnoble
  • 5,794
  • 3
  • 25
  • 32
  • quick question how would I get myImage into a UIWebView? – HurkNburkS May 23 '13 at 04:07
  • You could save the resulting image using `UIImagePNGRepresentation` and save the resulting `NSData` object using `writeToFile:atomically:` (or similar). Instantiate an `NSURLRequest` object referencing the image, and use the `UIImageView` method `loadRequest:`. Quick answer (-; – bobnoble May 23 '13 at 04:21
10

NSData Base64 library files will help you.

#import "NSData+Base64.h"

//Data from your string is decoded & converted to UIImage
UIImage *image = [UIImage imageWithData:[NSData
dataFromBase64String:strData]];

Hope it helps

Swift 3 Version

It's pretty much the same

//Create your NSData object
let 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)

Swift 2.3 Version

//Create your NSData object
let data = NSData(base64EncodedString: "yourStringData", options: .IgnoreUnknownCharacters)

//And then just create a new image based on the data object
let image = UIImage(data: data!)
aug2uag
  • 3,379
  • 3
  • 32
  • 53
  • 1
    awesome going to try now :) – HurkNburkS May 23 '13 at 03:51
  • i am getting an error when I try and use NSData*data .. etc which says this **No known class method for selector 'dataWithBase64EncodedString:'** – HurkNburkS May 23 '13 at 03:58
  • I think i pretty much have it.. one question how would i get this **UIImage *image = [UIImage imageWithData:data];** into a UIWebView? – HurkNburkS May 23 '13 at 04:06
  • This didnt really work but it is the post that helped me the most :) I had to use this code ** NSData *data = [[NSData alloc] initWithData:[NSData dataFromBase64String:xmlString]];** and to disp[lay on webview **[myWebView loadData:data MIMEType:@"image/png" textEncodingName:nil baseURL:nil];** – HurkNburkS May 23 '13 at 04:22
  • 5
    iOS7 has new methods for this purpose - (id)initWithBase64EncodedString:(NSString *)base64String options:(NSDataBase64DecodingOptions)options and - (NSString *)base64EncodedStringWithOptions:(NSDataBase64EncodingOptions)options – Vishal Singh Apr 29 '14 at 19:14
7
//retrieve your string 

NSString *string64 = //... some string base 64 encoded

//convert your string to data

NSData *data = [[NSData alloc] initWithBase64EncodedString:string64 options:NSDataBase64DecodingIgnoreUnknownCharacters];

//initiate image from data

UIImage *captcha_image = [[UIImage alloc] initWithData:data];
Prasanth S
  • 3,725
  • 9
  • 43
  • 75
GPY
  • 3,832
  • 1
  • 16
  • 11
1
 NSString *base64String=@"............";
    NSData* data = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
            UIImage* img = [UIImage imageWithData:data];

//imageview  is the refrence outlet of image view
            self.imageview.image=img;
0

This will work for ios7 and above.

- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
  NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
  return [UIImage imageWithData:data];
}

And use the code like this:

UIImage *img = [self decodeBase64ToImage:<ur base64 string>];
Vaibhav Saran
  • 12,848
  • 3
  • 65
  • 75
0

Swift 3 version

Use type Data instead of NSData

let data = Data(base64Encoded: imageBase64String, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)
let image = UIImage(data:data!)
rabenson
  • 9
  • 2