码迷,mamicode.com
首页 > Web开发 > 详细

XMPP 客户端开发 ----- 用户上传头像(四)

时间:2015-01-05 16:43:45      阅读:148      评论:0      收藏:0      [点我收藏+]

标签: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()];
    
    
}

在登录成功的回调中与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];
        
    }
    
    

    
}



原文地址:http://blog.csdn.net/qqmcy/article/details/42422893

XMPP 客户端开发 ----- 用户上传头像(四)

标签:ios   xmpp   头像   xmppvcardtemp   xmppvcardtempmodule   

原文地址:http://blog.csdn.net/qqmcy/article/details/42422893

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!