本文介绍了XMPPFramework - 上传个人资料或头像图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用 xmpp 框架的聊天应用程序.我能够通过 xmpp 发送或接收消息.但是当我尝试使用以下代码上传用户个人资料或头像时,没有成功.

I am working on the chat app in which I have used the xmpp framework. I am able to send or receive the messages through xmpp. But when I tried to upload the user profile or avatar image with the following code, didn't get success.

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:stringPath]];
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
UIImageView *thumbimage;
thumbimage.image = tmpImage;

NSData *imageData1 = UIImageJPEGRepresentation(thumbimage.image,0.5);
NSXMLElement *vCardXML = [NSXMLElement elementWithName:@"vCard" xmlns:@"vcard-temp"];
NSXMLElement *photoXML = [NSXMLElement elementWithName:@"PHOTO"];
NSXMLElement *typeXML = [NSXMLElement elementWithName:@"TYPE" stringValue:@"image/jpeg"];
NSXMLElement *binvalXML = [NSXMLElement elementWithName:@"BINVAL" stringValue:[[NSString alloc] initWithData:imageData1 encoding:NSUTF8StringEncoding]];

[photoXML addChild:typeXML];
[photoXML addChild:binvalXML];
[vCardXML addChild:photoXML];

XMPPvCardTemp *myvCardTemp = [[[self appDelegate] xmppvCardTempModule] myvCardTemp];

if (myvCardTemp) {
    [myvCardTemp setPhoto:imageData1];
    [[[self appDelegate] xmppvCardTempModule] updateMyvCardTemp:myvCardTemp];
}

如果我在代码中做错了什么,请帮助我将上传图像概念实现到 XMPP 或纠正我.

Please help me to implement the upload image concept to XMPP or correct me If I am doing something wrong in the code.

提前致谢.

推荐答案

您好,我使用您的代码成功上传了我的头像,但有一些更改:

Hello I succeeded to have my avatar uploaded using your code but with some changes:

UIImage *tmpImage = [UIImage imageNamed: @"myimage.jpg"];

NSData *imageData1 = UIImageJPEGRepresentation(tmpImage,0.0);

NSXMLElement *vCardXML = [NSXMLElement elementWithName:@"vCard" xmlns:@"vcard-temp"];
NSXMLElement *photoXML = [NSXMLElement elementWithName:@"PHOTO"];
NSXMLElement *typeXML = [NSXMLElement elementWithName:@"TYPE" stringValue:@"image/jpg"];

NSString *image64 = [self encodeToBase64String:tmpImage];

NSXMLElement *binvalXML = [NSXMLElement elementWithName:@"BINVAL" stringValue:image64];

[photoXML addChild:typeXML];
[photoXML addChild:binvalXML];
[vCardXML addChild:photoXML];


XMPPvCardTemp *myvCardTemp = [[[self appDelegate] xmppvCardTempModule] myvCardTemp];

if (myvCardTemp) {
    [myvCardTemp setPhoto:imageData1];
    [[[self appDelegate] xmppvCardTempModule] updateMyvCardTemp:myvCardTemp];
}

这个类按照 Keith 的建议转换为 base64

This class converts to base64 as Keith suggested

- (NSString *)encodeToBase64String:(UIImage *)image {
    return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

此外,我修改了 XMPPvCardTemp.h 以公开此方法:

futhermore, I modified XMPPvCardTemp.h to expose this method:

- (void)setPhoto:(NSData *)data;

无论如何,XEP-0054 扩展头

Anyway, the XEP-0054 extension header

#import "XMPPvCardTemp.h"

被请求.

也许这对某人有帮助.

这篇关于XMPPFramework - 上传个人资料或头像图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:15