0

我需要将图像保存在数据库中,它位于远程服务器上。为此,我定义了一个 BLOB。

NSData *imagenData = UIImagePNGRepresentation(self.imagen.image);

我的问题:当我从数据库接收到 NSData 并再次解析到 UIImage 以显示此图像时。

NSString imagenStr = [dict objectForKey:@"imagen"]; 
NSData imagenData = [imagenStr dataUsingEncoding:NSUTF8StringEncoding]; 
self.imagen.image = [UIImage imageWithData:imagenData];

图像不会在使用界面加载。为什么?对不起我的英语,谢谢。

4

2 回答 2

0

如果您将图像存储为 NSData,为了在 UI 上显示它应该很简单:

UIImage *image = [UIImage imageWithData:imagenData];
于 2013-05-15T20:19:56.730 回答
0

最后,我将 NSData 转换为 Base64。

NSData *imagenData = UIImagePNGRepresentation(self.imagen.image);
MetodosComunes *metodos = [[MetodosComunes alloc] init];
NSString *imagenStr = [metodos base64forData:imagenData];

base64forData 方法:

-(NSString*)base64forData:(NSData*)theData {

    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];

    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;

    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }

    return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
}

然后,我需要在应用程序中显示这个保存的图像,并将 base64 转换为 NSData:

- (NSData *)base64DataFromString: (NSString *)string
{
    unsigned long ixtext, lentext;
    unsigned char ch, inbuf[4], outbuf[3];
    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;
}

当我显示图像时,图像是黑色的。在控制台出现这个错误:

5 月 16 日 17:04:13 MacBook-Pro-de-Alejandro.local pantallas [1608]:ImageIO:PNG 无效 PNG 文件:iDOT 未指向有效的 IDAT 块 5 月 16 日 17:04:13 MacBook-Pro-de- Alejandro.local pantallas[1608] : ImageIO: PNG 额外压缩数据

于 2013-05-16T15:15:29.207 回答