2010-07-01 70 views
2

此問題與Iphone SDK,NSData和UIImage有關。如何從NSData創建UIImage和XMPP的頭像數據?

我試圖創建從阿凡達數據的圖像從像下面的XMPP返回:

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

因此,在這種情況下,我認爲a3f549fa9705e7ead2905de0b6a804227ecdd404是照片數據。 那麼我怎樣才能把它轉換成NSData?

我想如果我能得到的NSData對象,我可以很容易地創建UIImage,對不對?


我覺得 「a3f549fa9705e7ead2905de0b6a804227ecdd404」 是照片數據 這是我的代碼:

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

然而, 這是行不通的。 任何人都知道它有什麼問題嗎?

回答

1

這是圖片的哈希你現在必須發送vCard請求中包含與驗證和BINVAL包含以base64圖像數據相同的哈希

2

在XMPPPresence.m添加此方法

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

} 

//在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]; 
       } 
     } 

} 

//當好友會發送照片,這將是虛擬卡的base64編碼。 //你會收到它作爲智商:

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

} 

希望這會幫助你。

+0

請在運行之間緩存照片或電子名片數據。如果您每次登錄時都重新請求所有夥伴的電話卡,則會觸發許多服務器上的拒絕服務保護。 – 2014-06-11 14:31:42