问题
Hi I am making an app based on the chatting I can send chat messages, but if i add image attachment to the chat message, its not working.here is my message code
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];
I am using xmpp framework and openfire server. and i don't know how to send images.please help me and i am using sqlite for storing and fetching Thanks in advance.
回答1:
attachment element is used to send image in XMPP. you can use following function:
-(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];}
回答2:
In 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)
But If xml size is large, server will return size error. So First upload image to file server and then send url by xmpp protocol
回答3:
For sending a small image to friend you can use the below function, but for sending a large image/video/audio file, it is recommended to share large files via streams "out of band" and use XMPP as signaling protocol - you should just send http:// or rtp:// link, and your buddy's client will download it, or start playing audio/video from given stream.
You can use XEP-0066
: Out Of Band Data - simple extension, where you just attach URL to standard element.
Sending Image
//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];
}
at Receiver end
XMPPStream
Delegate 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;
}
}
来源:https://stackoverflow.com/questions/32946115/how-to-send-image-in-chat-usig-xmpp-framework