【问题标题】:How to Create UIImage from NSData and Avatar Data of XMPP?如何从 XMPP 的 NSData 和 Avatar 数据创建 UIImage?
【发布时间】:2011-03-10 19:47:06
【问题描述】:

这个问题与Iphone SDK、NSData和UIImage有关。

我正在尝试从 xmpp 返回的头像数据创建图像,如下所示:

<presence from='yyy@184.73.164.51/spark' to='ken@184.73.164.51/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];

然而, 它不起作用。 有谁知道这是怎么回事?

【问题讨论】:

    标签: iphone sdk uiimage xmpp nsdata


    【解决方案1】:

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

    // 并且当好友发送照片时,它将在 vcard BASE64-encoded 中。 // 你会收到它作为 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;
    
    }
    

    希望这会对您有所帮助。

    【讨论】:

    • 请在运行之间缓存照片或电子名片数据。如果您每次登录时都重新请求所有好友的电子名片,您将在许多服务器上触发拒绝服务保护。
    【解决方案2】:

    这是您现在必须发送一个 vcard 请求的图片哈希,该请求将包含相同的哈希用于验证和 binval,其中包含 base64 中的图片数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-13
      • 2014-12-28
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多