Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a base64 image within a UIImageView?

I got this Base64 gif image:

R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7 

Now I want to display this Base64 String within my IPhone App.

I could get this working by using the WebView:

aUIWebView.scalesPageToFit = NO; aUIWebView.opaque = NO; aUIWebView.backgroundColor = [UIColor clearColor];  [aUIWebView loadHTMLString:   @"<html><body style=""background-color: transparent""><img src=""data:image/png;base64,R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7"" /></body></html>"  baseURL:nil]; 

Is it possible to do the same thing by using the UIImageView?

like image 850
jantimon Avatar asked Sep 02 '09 10:09

jantimon


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

How does Base64 store images?

Instead, they are stored in their raw binary form in a binary (blob) column, or file. Base64 is only used as a transport mechanism, not for storage. For example, you can embed a base64 encoded image into an XML document or an email message. Base64 is also stream friendly.


1 Answers

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.

like image 54
masche Avatar answered Nov 13 '22 04:11

masche