如何从十六进制数据创建 UIIMage?

发布于 2024-09-08 09:10:38 字数 822 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

优雅的叶子 2024-09-15 09:10:38

该字符串有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文