如何从Hex Data创建UIIMage?

时间:2010-07-01 23:54:39

标签: objective-c uiimage nsdata

我正在使用XMPP,XMPP将为我提供如下照片数据: a3f549fa9705e7ead2905de0b6a804227ecdd404

所以我假设上面的数据是照片数据,我认为它是Hex数据。 (也许我错了)

所以我使用以下命令创建UIImage,但它不起作用 有谁知道怎么做?

我要做的是将命令从Hex String更改为NSData。

NSString* command = @"a3f549fa9705e7ead2905de0b6a804227ecdd404";
command = [command stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableData *commandToSend= [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [command length]/2; i++) {
    byte_chars[0] = [command characterAtIndex:i*2];
    byte_chars[1] = [command characterAtIndex:i*2+1];
    whole_byte = strtol(byte_chars, NULL, 16);
    [commandToSend appendBytes:&whole_byte length:1]; 
}

UIImage *image = [UIImage imageWithData: commandToSend];

1 个答案:

答案 0 :(得分:1)

字符串长度为40个字符,肯定看起来是十六进制编码的。这使得160位信息。根据这份文件,这160让我想起了SHA-1:

http://xmpp.org/extensions/xep-0153.html

所以你在这里得到的是图像的校验和,而不是图像本身。您需要阅读文档以了解如何获取整个图像。