问题UIImage initWithData

时间:2011-08-24 18:46:27

标签: iphone objective-c uiimage syntax-error nsdata

我有一个带有Base 64图像解码的NSData。 我想在UIImage中转换这些数据,但......

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 - >变量

我知道数据中的内容是正确的,但在我的控制台中:

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

我已使用此Objective C代码进行解码:

- (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;
}

和我用这个Java代码编码:

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");

1 个答案:

答案 0 :(得分:5)

NSData并没有神奇地解码base64,你必须自己做。有一个示例如何在this blog post中执行此操作。

相关问题