views:

28

answers:

1

Hi, This question is related to Iphone SDK, NSData and UIImage.

I am trying to create an image from the Avatar Data returned from the xmpp like the following:

<presence from='[email protected]/spark' to='[email protected]/424978324712783686768453' id='Oj02v-45'><status>Away due to idle.</status><priority>0</priority><show>away</show><x xmlns='vcard-temp:x:update'><photo>a3f549fa9705e7ead2905de0b6a804227ecdd404</photo></x><x xmlns='jabber:x:avatar'><hash>a3f549fa9705e7ead2905de0b6a804227ecdd404</hash></x></presence>

So in this case, I assume that a3f549fa9705e7ead2905de0b6a804227ecdd404 is the photo data. So How can I transfer this into NSData?

I think if I can get the NSData object, I can easily create the UIImage, right?

A: 

Hi, I think "a3f549fa9705e7ead2905de0b6a804227ecdd404" is the photo data this is my codes:

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];

However, it doesn't work. Anyone knows what's wrong with it?