2010-07-01 5 views
2

Cette question est liée à Iphone SDK, NSData et UIImage.Comment créer UIImage à partir de données NSData et Avatar de XMPP?

Je suis en train de créer une image à partir des données Avatar est revenu de la XMPP comme ce qui suit:

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

Donc dans ce cas, je suppose que a3f549fa9705e7ead2905de0b6a804227ecdd404 est les données de photos. Alors Comment puis-je transférer ceci dans NSData?

Je pense que si je peux obtenir l'objet NSData, je peux facilement créer le UIImage, non?


Je pense que "a3f549fa9705e7ead2905de0b6a804227ecdd404" est les données photo ceci est mon code:

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

Cependant, il ne fonctionne pas. Quelqu'un sait ce qui ne va pas?

Répondre

1

C'est le hachage de l'image que vous avez maintenant d'envoyer une demande vcard qui contiendra le même hachage pour la vérification et binval contenant les données d'image en base64

2

Dans XMPPPresence.m ajouter cette méthode

-(NSString *)photo { 
    NSXMLElement *xElement = [self elementForName:@"x" xmlns:@"vcard-temp:x:update"]; 
    NSString *photoHash = [[xElement elementForName:@"photo"]stringValue]; 
    return photoHash; 

} 

// dans le délégué XMPPStream:

- (void)xmppStream:(XMPPStream *)stream didReceivePresence: 
(XMPPPresence *)presence { 
     NSString *photoHash = [presence photo]; 
     if ([photoHash length] > 0) { // in case when there's no photo hash 
       XMPPJID *rosterJID = [presence from]; 
       BOOL requestPhoto = ... // determine if you need to request new 
photo or nor 
       if (requestPhoto) { 
         NSXMLElement *iqAvatar = [NSXMLElement elementWithName:@"iq"]; 

         NSXMLElement *queryAvatar = [NSXMLElement elementWithName:@"vCard" 
xmlns:@"vcard-temp"]; 
         [iqAvatar addAttributeWithName:@"type" stringValue:@"get"]; 
         [iqAvatar addAttributeWithName:@"to" stringValue:[rosterJID full]]; 
         [iqAvatar addChild:queryAvatar]; 

         XMPPIQ *avatarRequestIQ = [XMPPIQ iqFromElement:iqAvatar]; 
         [stream sendElement:avatarRequestIQ]; 
       } 
     } 

} 

// Et quand mon pote va envoyer une photo, il sera codé BASE64-vcard. // Vous recevrez comme IQ:

- (BOOL)xmppStream:(XMPPStream *)stream didReceiveIQ:(XMPPIQ *)iq { 
     XMPPElement *vCardPhotoElement = (XMPPElement *)[[iq 
elementForName:@"vCard"] elementForName:@"PHOTO"]; 
     if (vCardPhotoElement != nil) { 
       // avatar data 
       NSString *base64DataString = [[vCardPhotoElement 
elementForName:@"BINVAL"] stringValue]; 
       NSData *imageData = [NSData 
dataFromBase64String:base64DataString]; // you need to get NSData 
BASE64 category 
       UIImage *avatarImage = [UIImage imageWithData:imageData]; 

       XMPPJID *senderJID = [iq from]; 
       [self xmppStream:stream didReceiveImage:avatarImage 
forBuddy:senderJID]; // this is my custom delegate method where I 
save new avatar to cache 
     } 
     return NO; 

} 

Espérons que cela vous aidera.

+0

VEUILLEZ mettre en cache les données de la photo ou de la vcard entre les exécutions. Si vous demandez à nouveau toutes les vCard de vos amis à chaque connexion, vous déclenchez les protections de déni de service sur de nombreux serveurs. –

Questions connexes