如何从十六进制数据创建 UIIMage?
我正在使用 XMPP,XMPP 会给我如下的照片数据: a3f549fa9705e7ead2905de0b6a804227ecdd404
所以我假设上面的数据是照片数据,并且我假设它是十六进制数据。 (也许我错了)
所以我使用以下内容来创建 UIImage,但它不起作用 有人知道该怎么做吗?
我想做的是将命令从十六进制字符串更改为 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];
I am using XMPP and XMPP will give me a photo data like the following:
a3f549fa9705e7ead2905de0b6a804227ecdd404
So I assume the above data is the photo data and I assume that it's Hex data. (maybe I am wrong)
So I use the following to create the UIImage, but it doesn't work
anyone know how to do it?
What I am trying to do is to change command from Hex String into 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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该字符串有 40 个字符长,看起来肯定是十六进制编码的。这产生了 160 位信息。这个 160 让我想起了 SHA-1,根据这个文档:
http://xmpp。 org/extensions/xep-0153.html
所以你这里得到的是图像的校验和,而不是图像本身。您需要阅读文档以了解如何获取整个图像。
The string is 40 characters long and surely looks hex-encoded. This makes 160 bits of information. And this 160 reminds me of SHA-1, in accordance with this document:
http://xmpp.org/extensions/xep-0153.html
So what you have here is the checksum of an image, not the image itself. You need to read the document to find out how to get the whole image.