即使我没有将图像直接发送到 xmpp,因为这需要很多时间。
在我的项目中,我的消息可以是字符串,也可以是图像,我发现只有一种方法可以发送消息,即。
[xmppRoom sendMessageWithBody:@"Your msg"];
所以对于发送图像,首先我将其转换为 base64,然后将其上传到我的服务器并从服务器获取该图像的 URL。一旦我们获得 URL,只需在上述方法中传递该 URL。
我面临的问题是如何隔离普通消息和 URL(图片)
因此,对于发送普通文本,我直接在上面的函数中发送它,但是对于发送 URL,我正在发送 nsdictionary,即我正在将 nsdictionary 转换为字符串并在上面的函数中发送
NSDictionary *dict = @{@"type":@"image",
@"url":@"Url of your image"};
NSString *newMessage = [NSString stringWithFormat:@"%@",dict];
[appDelegate.xmppRoom sendMessageWithBody:newMessage];
用于隔离普通消息和图像 -
- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message
{
messages *msg = [messages new];
NSDictionary *dictOfMedia = [NSPropertyListSerialization
propertyListWithData:[[[message elementForName:@"body"] stringValue] dataUsingEncoding:NSUTF8StringEncoding]
options:kNilOptions
format:NULL
error:NULL];
if ([dictOfMedia objectForKey:@"type"])
{
msg.mediaType = [dictOfMedia objectForKey:@"type"];
msg.url = [dictOfMedia objectForKey:@"url"];
}
else
{
msg.msg = [[message elementForName:@"body"] stringValue];
}
}
messages 是我的模型类,您可以简单地使用 nsstring 进行测试。
之后,只需使用任何开源项目下载图像或自行执行延迟加载。
希望对你有帮助:)