0

I have a NSData with a image decode of Base 64. I want to convert this data in UIImage but ....

NSData * data = [[NSData alloc] initWithContentsOfFile:@"/Users/.../Library/Caches/images/david.txt"];

UIImage * i = [[UIImage alloc] initWithData:data];

NSLog(@"%@",i);


NSData *datai = UIImagePNGRepresentation(i);

UIImage * i2 = [[UIImage alloc] initWithData:datai];


imagen = [[UIImageView alloc] initWithImage:i2];

[imagen setFrame:CGRectMake(0, 0, 320, 480)];

imagen --> variable

I know that the content in data is correct but in my console :

[Session started at 2011-08-24 20:44:25 +0200.]
2011-08-24 20:44:30.579 P[50194:207] (null)

I have decoded with this Objective C code:

- (NSData *)base64DataFromString: (NSString *)string
{
    unsigned long ixtext, lentext;
    unsigned char ch, inbuf[3], outbuf[4];
    short i, ixinbuf;
    Boolean flignore, flendtext = false;
    const unsigned char *tempcstring;
    NSMutableData *theData;

    if (string == nil)
    {
        return [NSData data];
    }

    ixtext = 0;

    tempcstring = (const unsigned char *)[string UTF8String];
    lentext = [string length];

    theData = [NSMutableData dataWithCapacity: lentext];

    ixinbuf = 0;

    while (true)
    {
        if (ixtext >= lentext)
        {
            break;
        }

        ch = tempcstring [ixtext++];

        flignore = false;

        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 = true;
        }
        else if (ch == '/')
        {
            ch = 63;
        }
        else
        {
            flignore = true; 
        }

        if (!flignore)
        {
            short ctcharsinbuf = 3;
            Boolean flbreak = false;

            if (flendtext)
            {
                if (ixinbuf == 0)
                {
                    break;
                }

                if ((ixinbuf == 1) || (ixinbuf == 2))
                {
                    ctcharsinbuf = 1;
                }
                else
                {
                    ctcharsinbuf = 2;
                }

                ixinbuf = 3;

                flbreak = true;
            }

            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++)
                {
                    [theData appendBytes: &outbuf[i] length: 1];
                }
            }

            if (flbreak)
            {
                break;
            }
        }
    }

    return theData;
}

and i encoded with this Java code:

File f = new File(url);
BufferedInputStream bi = new BufferedInputStream(new FileInputStream(f));
int bytes = (int) f.length();
byte[] buffer = new byte[bytes];
int readBytes = bi.read(buffer);
bi.close();

/*BASE 64 ENCODE*/
 byte[] encodedString = Base64.encode(buffer);
 String image = new String(encodedString, "UTF8");
David
  • 45
  • 1
  • 5
  • 1
    Make sure you [decode the base64](http://stackoverflow.com/questions/392464/any-base64-library-on-iphone-sdk) text before trying to create a UIImage with it. – Joe Aug 24 '11 at 18:49

1 Answers1

5

NSData doesn't magically decode base64, you'll have to do that yourself. There's an example how to do this in this blog post.

omz
  • 53,243
  • 5
  • 129
  • 141