views:

507

answers:

3

Hey,

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?

+2  A: 
  1. Decode Base64 to raw binary. You're on your own here. You might find cStringUsingEncoding: of NSString useful.
  2. Create NSData instance using dataWithBytes:length:
  3. Use [UIImage imageWithData:] to load it.
porneL
+2  A: 

Thanks porneL

I could find a Base64 script using google: Base64.m

So I modified it a little bit and could get a working test code:

NSString * base64img = @"R0lGODlhDAAMALMBAP8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAUKAAEALAAAAAAMAAwAQAQZMMhJK7iY4p3nlZ8XgmNlnibXdVqolmhcRQA7";
base64Data = [base64img dataUsingEncoding:NSASCIIStringEncoding];
base64Bytes = [base64Data bytes];
mutableData = [NSMutableData dataWithCapacity:[base64Data length]];
lentext = [base64Data length];

while( YES ) {
       if( ixtext >= lentext ) break;
       ch = base64Bytes[ixtext++];
       flignore = NO;

       if( ( ch >= 'A' ) && ( ch <= 'Z' ) ) ch = ch - 'A';
       else if( ( ch >= 'a' ) && ( ch <= 'z' ) ) ch = ch - 'a' + 26;
       else if( ( ch >= '0' ) && ( ch <= '9' ) ) ch = ch - '0' + 52;
    else if( ch == '+' ) ch = 62;
       else if( ch == '=' ) flendtext = YES;
       else if( ch == '/' ) ch = 63;
       else flignore = YES;

       if( ! flignore ) {
     short ctcharsinbuf = 3;
     BOOL flbreak = NO;

     if( flendtext ) {
      if( ! ixinbuf ) break;
      if( ( ixinbuf == 1 ) || ( ixinbuf == 2 ) ) ctcharsinbuf = 1;
      else ctcharsinbuf = 2;
      ixinbuf = 3;
      flbreak = YES;
     }

     inbuf [ixinbuf++] = ch;

     if( ixinbuf == 4 ) {
      ixinbuf = 0;
      outbuf [0] = ( inbuf[0] << 2 ) | ( ( inbuf[1] & 0x30) >> 4 );
      outbuf [1] = ( ( inbuf[1] & 0x0F ) << 4 ) | ( ( inbuf[2] & 0x3C ) >> 2 );
      outbuf [2] = ( ( inbuf[2] & 0x03 ) << 6 ) | ( inbuf[3] & 0x3F );

      for( i = 0; i < ctcharsinbuf; i++ )
       [mutableData appendBytes:&outbuf[i] length:1];
     }

     if( flbreak )  break;
       }
}


//Finally I can access my image data:
aUIImageView.image = [UIImage imageWithData: [NSData dataWithData:mutableData]];
Ghommey
A: 

You dont 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];

But i dont know if this the best way.

Markus Scheucher