ios - 如何在 iphone 上使用 xmpp 框架将图像从一台设备传输到另一台设备?

标签 ios objective-c iphone xmppframework

我使用 xmpp 协议(protocol)制作了一对一聊天应用程序。现在我想在我的应用程序中发送图像和视频。我搜索了很多关于文件传输的信息,但没有找到解决方案。我正在使用以下代码进行套接字连接。

请帮助我如何做到这一点。

我的代码是:

[TURNSocket setProxyCandidates:@[@"MyserverHost-desktop"]]; 

XMPPJID *jid = [XMPPJID jidWithString:@"1254225445@MyserverHost-desktop"];

 TURNSocket *turnSocket = [[TURNSocket alloc] initWithStream:[[self appDelegate]xmppStream] toJID:jid]; 

[app.turnSocketArray addObject:turnSocket]; 

[turnSocket startWithDelegate:self delegateQueue:dispatch_get_main_queue()]; 
[turnSocket release]; 

- (void)turnSocket:(TURNSocket *)sender didSucceed:(GCDAsyncSocket *)socket 
{ 

}
- (void)turnSocketDidFail:(TURNSocket *)sender
{

}

每次连接失败方法调用..

谢谢。

最佳答案

即使我没有直接将图像发送到 xmpp,因为它需要很多时间。

在我的项目中,我的消息可以是字符串,也可以是图像,我发现只有一种方法可以发送消息,即..

[xmppRoom sendMessageWithBody:@"Your msg"];

因此,为了发送图像,首先我将其转换为 base64,然后将其上传到我的服务器并从服务器获取该图像的 URL。一旦我们获得 URL,只需在 Above 方法中传递该 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 进行测试。

在此之后,只需使用任何开源项目下载图像或自行执行延迟加载。

希望这对你有帮助:)

关于ios - 如何在 iphone 上使用 xmpp 框架将图像从一台设备传输到另一台设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27038538/

相关文章:

ios - rangeOfString 因 CLPlacemark 值而失败

iphone - 在没有分发文件的情况下将 ipa 发送给测试人员(仅带有配置文件)

iphone - viewcontroller 中 appdelegate 和 delegate 的区别

ios - 如何在 .Ended 状态下访问 Pan Gesture 的初始点

ios - Crashreport 指示对象和参数之间的 KVO 连接从未在代码中启动

objective-c - 从 NSMutableArray 中删除对象

iphone - Xcode 5 部署目标仅限于 iOS 7

ios - Swift:从 View 中获取值(value)的最佳方式

iphone - 阴影到透明的 UITableView 和圆角

objective-c - Cocoa Programming,设置委托(delegate)