XMPP 客户端开发 ----- 用户上传头像(四)
这篇记录下我利用XMPP实现上传头像的过程,发现XMPP真心很强大很多功能都已经实现了。我们只要直接调用就可以了。下面上代码了。
引入头文件 这个头文件需要自己导入不然 XMPPvCardTemp 下的方法调用不了。
#import "XMPPvCardTemp.h"
声明相关对象
// 声明上传头像相关对象 @property (nonatomic , strong) XMPPvCardCoreDataStorage *xmppvCardStorage; @property (nonatomic , strong) XMPPvCardTempModule *xmppvCardTempModule; @property (nonatomic , strong) XMPPvCardAvatarModule *xmppvCardAvatarModule;
初始化相关对象
// 初始化方法 - (void)setupStream { _xmppStream = [[XMPPStream alloc] init]; [_xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()]; _xmppRosterDataStorage = [[XMPPRosterCoreDataStorage alloc] init]; _xmppRoster = [[XMPPRoster alloc] initWithRosterStorage:_xmppRosterDataStorage]; // _xmppRoster.autoFetchRoster = YES; // _xmppRoster.autoAcceptKnownPresenceSubscriptionRequests = YES; // _xmppvCardStorage = [XMPPvCardCoreDataStorage sharedInstance]; _xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:_xmppvCardStorage]; _xmppvCardAvatarModule = [[XMPPvCardAvatarModule alloc] initWithvCardTempModule:_xmppvCardTempModule]; [_xmppvCardTempModule addDelegate:self delegateQueue:dispatch_get_main_queue()]; [_xmppvCardAvatarModule addDelegate:self delegateQueue:dispatch_get_main_queue()]; }
在登录成功的回调中与XMPPStream对象进行关联
// 登陆成功 - (void) xmppStreamDidAuthenticate:(XMPPStream *)sender { [self goOnline]; NSLog(@"登陆成功"); [[NSUserDefaults standardUserDefaults] setObject:@"success" forKey:XMPPLogin]; [_xmppRoster activate:_xmppStream]; [_xmppRoster addDelegate:self delegateQueue:dispatch_get_main_queue()]; [_xmppvCardTempModule activate:_xmppStream]; [_xmppvCardAvatarModule activate:_xmppStream]; [self queryRoster]; }
上传头像的代码
- (void) offeringProfile { NSXMLElement *vCardXML = [NSXMLElement elementWithName:@"vCard" stringValue:@"vcard-temp"]; NSXMLElement *photoXML = [NSXMLElement elementWithName:@"PHOTO"]; NSXMLElement *typeXML = [NSXMLElement elementWithName:@"TYPE" stringValue:@"image/jpeg"]; UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"changmen" ofType:@"jpg"]]]; NSData *dataFromImage = UIImageJPEGRepresentation(image, 0.7f); NSXMLElement *binvalXML = [NSXMLElement elementWithName:@"BINVAL" stringValue:[dataFromImage base64Encoding]]; [photoXML addChild:typeXML]; [photoXML addChild:binvalXML]; [photoXML addChild:photoXML]; XMPPvCardTemp * myvCardTemp = [_xmppvCardTempModule myvCardTemp]; NSLog(@"%@",myvCardTemp); if (myvCardTemp) { myvCardTemp.photo = dataFromImage; [_xmppvCardTempModule updateMyvCardTemp:myvCardTemp]; }else { XMPPvCardTemp *newvCardTemp = [XMPPvCardTemp vCardTempFromElement:vCardXML]; newvCardTemp.nickname = @"nick"; [_xmppvCardTempModule updateMyvCardTemp:newvCardTemp]; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。