iphone - 如何从XMPP的NSData和头像数据创建UIImage?

标签 iphone sdk uiimage xmpp nsdata

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

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

<presence from='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="136a6a6a53222b273d24203d2225273d2622" rel="noreferrer noopener nofollow">[email protected]</a>/spark' to='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="78131d163849404c564f4b56494e4c564d49" rel="noreferrer noopener nofollow">[email protected]</a>/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];

但是, 它不起作用。 有人知道这是怎么回事吗?

最佳答案

在XMPPPresence.m中添加此方法

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

}

//在 XMPPStream 的委托(delegate)中:

- (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 编码。 //您将收到它作为 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;

}

希望这对您有帮助。

关于iphone - 如何从XMPP的NSData和头像数据创建UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3161180/

相关文章:

c++ - directx 9 与 Visual Studio 2012 Express

ios - 如何在 UIImage 中创建透明区域?

iphone - 如何判断请求何时完成(ASIHTTPRequest)?

java - 未找到 sdk_config.properties

iphone - 更改 UIWebView 的文本颜色

iphone - 如何在Xcode中用+替换空格

ios - 如何在 UIImageView 中居中缩放(旋转)UIImage

cocoa - NSImage 或 CGImage 的最大位图图像大小?

iphone - iOS:addConstraints:应用程序崩溃

iphone - Obj C 在 sem-singleton 和 child 之间通信的两种方式