标签:ios xmpp 头像 xmppvcardtemp xmppvcardtempmodule
这篇记录下我利用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()]; }
// 登陆成功 - (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]; } }
原文地址:http://blog.csdn.net/qqmcy/article/details/42422893
标签:ios xmpp 头像 xmppvcardtemp xmppvcardtempmodule
原文地址:http://blog.csdn.net/qqmcy/article/details/42422893