如何在chat usig xmpp框架中发送图像

时间:2015-10-05 10:14:56

标签: ios objective-c iphone xmpp chat

您好我正在根据我可以发送聊天消息的聊天制作应用程序,但是如果我在聊天消息中添加图像附件,则无法正常工作。这是我的消息代码

if (![[messageStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] && imgURL==nil) {
      return;
}
NSString *MyString;
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm,yyyy/MM/dd"];
MyString = [dateFormatter stringFromDate:now];
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSString *myJID=[defaults valueForKey:@"userNameJID"];

    if ([imgURL length]==0) {
       chatMessageToSendAndPutInDB=[messageStr mutableCopy];
       chatMessageToSendAndPutInDB=[[chatMessageToSendAndPutInDB stringByReplacingOccurrencesOfString:@"'" withString:@""] mutableCopy];
    }else
    {

       receivedDataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
          chatMessageToSendAndPutInDB=[receivedDataString mutableCopy];
        chatMessageToSendAndPutInDB=[[chatMessageToSendAndPutInDB stringByReplacingOccurrencesOfString:@"'" withString:@""] mutableCopy];

    }
    [DBObject open];
[DBObject insertInTable:@"chatTable" withUserId:myJID withFriendID:[[ASNGlobalClass shareManager] chatToUserJID] withMessage:chatMessageToSendAndPutInDB withTimeDate:MyString withLeftOrRight:0];

 NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[DBObject closeDatabase];
[self fetchDataFromDatabase];
   if([messageStr length] > 0 ) {

  [body setStringValue:messageStr];
   }else if ([imgURL length]>0){
             [body setStringValue:receivedDataString];
   }    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"type" stringValue:@"chat"];

[message addAttributeWithName:@"to" stringValue:chatWithUser];
 NSXMLElement *chatStatus=[NSXMLElement elementWithName:@"composing" xmlns:UIApplicationStateActive];
[message addChild:chatStatus];
[message addChild:body];
[[[self appDelegate] xmppStream] sendElement:message];

我正在使用xmpp框架和openfire服务器。我不知道如何发送images.please帮助我,我正在使用sqlite存储和获取 提前谢谢。

3 个答案:

答案 0 :(得分:1)

attachment元素用于在XMPP中发送图像。 你可以使用以下功能:

-(void)sendMessage:(NSString *)strText img:(UIImage *)SendImage{
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:strText];

NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"type" stringValue:@"chat"];
[message addAttributeWithName:@"to" stringValue:userId];
[message addChild:body];

if (SendImage) {
    NSData *dataF = UIImagePNGRepresentation(SendImage);
    NSString *imgStr=[dataF base64EncodedStringWithOptions:0];

    NSXMLElement *ImgAttachement = [NSXMLElement elementWithName:@"attachement"];
    [ImgAttachement setStringValue:imgStr];
    [message addChild:ImgAttachement];
}

[self.xmppStream sendElement:message];
[self.view endEditing:YES];}

答案 1 :(得分:0)

在Swift3.0中

    let data = UIImageJPEGRepresentation(image, 0.1)
    let imageStr = data?.base64EncodedString()

    let body = DDXMLElement.element(withName: "body") as! DDXMLElement
    let messageID = XMPPManager.sharedInstance.xmppStream?.generateUUID()
    let imageAttachement = DDXMLElement.element(withName: "attachment", stringValue: imageStr!) as! DDXMLElement

    let message = DDXMLElement.element(withName: "message") as! DDXMLElement
    message.addAttribute(withName: "type", stringValue: "chat")
    message.addAttribute(withName: "id", stringValue: messageID!)
    message.addAttribute(withName: "to", stringValue: buddyJID.full())
    message.addChild(body)
    message.addChild(imageAttachement)

    XMPPManager.sharedInstance.xmppStream?.send(message)

但是如果xml大小很大,服务器将返回大小错误。 首先将图像上传到文件服务器,然后通过xmpp协议发送URL

答案 2 :(得分:0)

要向朋友发送小图片,您可以使用以下功能,但是对于发送大图像/视频/音频文件,建议通过“带外”流共享大文件,并使用XMPP作为信令协议 - 你应该发送http://或rtp://链接,你的伙伴的客户端会下载它,或者从给定的流开始播放音频/视频。

您可以使用XEP-0066:带外数据 - 简单扩展,只需将URL附加到标准元素。

发送图片

//Send Image to friend with message.
-(void)sendMessageWithImage:(NSString *)strText img:(UIImage *)SendImage {

    //Message string with image.
    NSString *messageStr = strText;

    NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
    [body setStringValue:messageStr];

    NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
    [message addAttributeWithName:@"type"stringValue:@"chat"];

    //set "to" string value to friend jid
    [message addAttributeWithName:@"to"stringValue:@"rohit@192.168.0.1"];
    [message addChild:body];

    NSData *dataPic =  UIImagePNGRepresentation(SendImage);

    NSXMLElement *photo = [NSXMLElement elementWithName:@"PHOTO"];
    NSXMLElement *binval = [NSXMLElement elementWithName:@"BINVAL"];
    [photo addChild:binval];

    //convert image to base64String
    NSString *base64String = [dataPic base64EncodedStringWithOptions:0];
    [binval setStringValue:base64String];
    [message addChild:photo];

    [[[self appDelegate] xmppStream] sendElement:message];

}

在Receiver end

XMPPStream代表Methode。

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {

    [self MessageReceive:message];

}

#pragma mark - Receive New Message Methode.
-(void)MessageReceive:(XMPPMessage *)message
{
    //Get friend XMPPUserCoreDataStorageObject object
    XMPPUserCoreDataStorageObject *user = [xmppRosterStorage userForJID:[message from]
                                                             xmppStream:xmppStream
                                                   managedObjectContext:[self managedObjectContext_roster]];

    //Get Message if friend send with image.
    NSString *body = [[message elementForName:@"body"] stringValue];

    //Get Display name of friend.
    NSString *displayName = [user jidStr];

    //Check friend send image or only text message
    if ([message elementForName:@"PHOTO"]) {

        //Get Image from base64String
        NSString *photoHash = [[message elementForName:@"PHOTO"] stringValue];
        NSData* data = [photoHash dataUsingEncoding:NSUTF8StringEncoding];
        NSData *nsdataDecoded = [data initWithBase64EncodedData:data options:0];

        //Here you can get image.
        UIImage *image = [UIImage imageWithData:nsdataDecoded];

    } else {

        //Here you can get friend text message
        NSString *message = body;
    }
}