简体   繁体   English

问题UIImage initWithData

[英]Problem UIImage initWithData

I have a NSData with a image decode of Base 64. I want to convert this data in UIImage but .... 我有一个NSData,其图像解码为Base64。我想在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 --> variable imagen->变量

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: 我已经用这个目标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;
}

and i encoded with this Java code: 我用这个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");

NSData doesn't magically decode base64, you'll have to do that yourself. NSData不会神奇地解码base64,您必须自己做。 There's an example how to do this in this blog post . 这篇博客文章中有一个如何执行此操作的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM