我从网络服务中得到这个十六进制
http://www.moodpin.it/cms/listar_avatar?id=9
并尝试转换为 NSData 并设置 UIImageView 但它不起作用
这是我的代码:
NSData *dataImage = [appDelegate hexStringToData:avatar.avatar];
[imageView setImage:[UIImage imageWithData:dataImage]];
- (NSData *) hexStringToData:(NSString *) aString
{
NSString *command = aString;
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];
}
NSLog(@"%@", commandToSend);
return commandToSend;
}