How to display a base64 image within a UIImageView? How to display a base64 image within a UIImageView? objective-c objective-c

How to display a base64 image within a UIImageView?


You don't have to encode it. Simply make a NSUrl, it knows the "data:"-url.

NSURL *url = [NSURL URLWithString:base64String];    NSData *imageData = [NSData dataWithContentsOfURL:url];UIImage *ret = [UIImage imageWithData:imageData];

As mentioned in the comments, you have to make sure that you prepend your data with data:image/png;base64, or else your base64 data is useless.


Very old question, but as of iOS7 there is a new, much easier way to do so, hence I'm writing it here so future readers can use this.

NSData* data = [[NSData alloc] initWithBase64EncodedString:base64String options:0];UIImage* image = [UIImage imageWithData:data];

Very easy to use, and will not hit the 2048 byte size limit of a URL.


This Code will display an base64 encoded string as a picture:

NSString *str = @"data:image/jpg;base64,";str = [str stringByAppendingString:restauInfo.picture];NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];

restauInfo.picture is an NSString which contains the base64 encoded JPG

In my case the Value from "restauInfo.picture" comes from a database