如何在ios上的xmpp服务器上传个人资料图片?

时间:2016-11-26 06:50:31

标签: ios objective-c base64 xmppframework avatar

 filePath = [Imagesdatapath1 stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",imageName]];
 NSError *errror;
   NSData *pngData = UIImagePNGRepresentation(resizedImage);


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


  NSString *strEncoded = [Base64 encode:pngData];

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

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

 XMPPvCardTemp *myvCardTemp = [XMPPvCardTempModule myvCardTemp];

 if (myvCardTemp) {
    [myvCardTemp setPhoto:pngData];
    [ XMPPvCardTempModule updateMyvCardTemp:myvCardTemp];
 }

     [pngData writeToFile:filePath atomically:YES];

 [[NSUserDefaults standardUserDefaults] setObject:Imagesdatapath1 forKey:@"primagepath"];
   [[NSUserDefaults standardUserDefaults] synchronize];

     [self uploadProfileImage:filePath];

请参阅上面的代码我正在使用聊天应用。我不知道xmpp图片上传头像的概念。我想在xmpp服务器和本地数据库上传个人资料图片(头像)。所以,我导入了像Base64XMPPvCardTempModule这样的库,但直到它出现了很多库错误以及V卡的情况。

1 个答案:

答案 0 :(得分:1)

1)首先,从“相机”或“ Gallary”中选择图像,然后将其调整为所需的格式:

NSData *img = [NSData dataWithData:UIImagePNGRepresentation(resizedImage)];

2)将XMPP值如下:

            NSXMLElement *vCardXML = [NSXMLElement elementWithName:@"vCard" xmlns:@"vcard-temp"];
            NSXMLElement *jidElement = [NSXMLElement elementWithName:@"JABBERID" objectValue:"your_jabberid_here"];
            NSXMLElement *profileUrlElement = [NSXMLElement elementWithName:@"PROFILEURL" stringValue:"server_file_path_of_image"];
            NSXMLElement *emailElement = [NSXMLElement elementWithName:@"EMAIL"];
            NSXMLElement *userIdElement = [NSXMLElement elementWithName:@"USERID" objectValue:"optional_if_you_want_set_email_id"];
            NSXMLElement *userNickname = [NSXMLElement elementWithName:@"NICKNAME" objectValue:"your_nickName_here"];

3)如下将每个NSXMLElement标签添加到vCard中:

        [emailElement addChild:userIdElement];
        [emailElement addChild:[NSXMLElement elementWithName:@"INTERNET"]];
        [emailElement addChild:[NSXMLElement elementWithName:@"PREF"]];
        [vCardXML addChild:jidElement];
        [vCardXML addChild:emailElement];
        [vCardXML addChild:profileUrlElement];
        [vCardXML addChild:userNickname];

4)最后,如下更新您的vCard:

XMPPvCardTemp *newvCardTemp = [XMPPvCardTemp vCardTempFromElement:vCardXML];
            [newvCardTemp setPhoto:img];
            [self->xmppvCardTempModule updateMyvCardTemp:newvCardTemp];

您可以通过以下方式检查新设置的vcard照片:

 XMPPvCardCoreDataStorage *  xmppvCardStorage = [XMPPvCardCoreDataStorage sharedInstance];
  XMPPvCardTempModule * xmppvCardTempModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:xmppvCardStorage];
    [xmppvCardTempModule  activate:[self xmppStream]];
   XMPPvCardTemp * vcard = [xmppvCardTempModule vCardTempForJID:[XMPPJID jidWithString:"your_jid_here" shouldFetch:YES];
    if (vcard.photo) {

        // YOUR_code_goes_here //
    }