此問題與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];
然而, 這是行不通的。 任何人都知道它有什麼問題嗎?
請在運行之間緩存照片或電子名片數據。如果您每次登錄時都重新請求所有夥伴的電話卡,則會觸發許多服務器上的拒絕服務保護。 – 2014-06-11 14:31:42