如何显示十六进制字符串[iphone]中的图像?

如何显示十六进制字符串[iphone]中的图像?,iphone,uiimage,hex,nsdata,Iphone,Uiimage,Hex,Nsdata,我有这样的十六进制代码89504E470D0A1A0A0000000D4948452000001000000010008060000005C72A8 现在这个十六进制代码是一些图像的代码。我知道,现在我想在imageView中显示该图像。我不知道怎么做,因为当我把这个十六进制转换成NSData,并试图把那个数据转换成图像视图,当我显示那个图像时。图像未显示 有人能告诉我怎么做吗 //这是第一部分,即图像到十六进制的转换 UIImage *img = [UIImage imageNamed:@"i

我有这样的十六进制代码89504E470D0A1A0A0000000D4948452000001000000010008060000005C72A8

现在这个十六进制代码是一些图像的代码。我知道,现在我想在imageView中显示该图像。我不知道怎么做,因为当我把这个十六进制转换成NSData,并试图把那个数据转换成图像视图,当我显示那个图像时。图像未显示

有人能告诉我怎么做吗

//这是第一部分,即图像到十六进制的转换

UIImage *img = [UIImage imageNamed:@"image.png"];
NSData *data1 = UIImagePNGRepresentation(img);
//NSLog(@“数据为%@”,数据1)


如果我正确理解了你的问题,
UIImage*image=[UIImage-imageWithData:data]是您所需要的

您的NSData到十六进制的转换不正确,您只使用前8个字节作为十六进制标记;请改用以下代码:

- (NSString*)stringWithHexBytes:(NSData *) data {
    static const char hexdigits[] = "0123456789ABCDEF";
    const size_t numBytes = [data length];
    const unsigned char* bytes = [data bytes];
    char *strbuf = (char *)malloc(numBytes * 2 + 1);
    char *hex = strbuf;
    NSString *hexBytes = nil;

    for (int i = 0; i<numBytes; ++i) {
        const unsigned char c = *bytes++;
        *hex++ = hexdigits[(c >> 4) & 0xF];
        *hex++ = hexdigits[(c ) & 0xF];
    }
    *hex = 0;
    hexBytes = [NSString stringWithUTF8String:strbuf];
    free(strbuf);
    return hexBytes;
}
-(NSString*)stringWithHexBytes:(NSData*)数据{
静态常量字符十六进制数字[]=“0123456789ABCDEF”;
const size_t numBytes=[数据长度];
常量无符号字符*字节=[数据字节];
char*strbuf=(char*)malloc(numBytes*2+1);
char*hex=strbuf;
NSString*hexBytes=nil;
对于(int i=0;i>4)&0xF];
*十六进制++=十六进制数字[(c)和0xF];
}
*十六进制=0;
hexBytes=[NSString stringWithUTF8String:strbuf];
免费(strbuf);
返回十六字节;
}
将十六进制字符串转换回NSData:

- (NSData *) hexStringToData:(NSString *) hexString
{
    unsigned char whole_byte;
    NSMutableData *returnData= [[NSMutableData alloc] init];
    char byte_chars[3] = {'\0','\0','\0'};
    int i;
    for (i=0; i < 8; i++) {
        byte_chars[0] = [hexString characterAtIndex:i*2];
        byte_chars[1] = [hexString characterAtIndex:i*2+1];
        whole_byte = strtol(byte_chars, NULL, 16);
        [returnData appendBytes:&whole_byte length:1]; 
    }
    return (NSData *) [returnData autorelease];
}
-(NSData*)hexStringToData:(NSString*)hexString
{
无符号字符整字节;
NSMutableData*returnData=[[NSMutableData alloc]init];
字符字节_chars[3]={'\0','\0','\0'};
int i;
对于(i=0;i<8;i++){
字节_字符[0]=[hexString字符索引:i*2];
byte_chars[1]=[hexString characterAtIndex:i*2+1];
整字节=strtol(字节字符,空,16);
[returnData appendBytes:&整个字节长度:1];
}
返回(NSData*)[returnData自动返回];
}

您需要了解更多关于图像表示的信息,例如:它是什么格式,它来自哪里?嘿,贾斯汀。我知道这一点,因为我有完整的形象。我必须这样做,就像在发送端,我必须把它转换成十六进制,在接收端,我必须把十六进制转换成UIImage。第一部分是明确的,另一部分没有发生:(好的-然后你应该添加你的图像到十六进制转换实现,这样我们就可以告诉你如何做反向操作。
- (NSData *) hexStringToData:(NSString *) hexString
{
    unsigned char whole_byte;
    NSMutableData *returnData= [[NSMutableData alloc] init];
    char byte_chars[3] = {'\0','\0','\0'};
    int i;
    for (i=0; i < 8; i++) {
        byte_chars[0] = [hexString characterAtIndex:i*2];
        byte_chars[1] = [hexString characterAtIndex:i*2+1];
        whole_byte = strtol(byte_chars, NULL, 16);
        [returnData appendBytes:&whole_byte length:1]; 
    }
    return (NSData *) [returnData autorelease];
}