我们如何将图像或照片上传到ejabberd XMPP Web服务器?

时间:2016-12-27 14:48:28

标签: ios swift xmpp ejabberd xmppframework

我在这里使用iOS XMPP库(https://github.com/processone/demo-xmpp-ios)作为我的聊天应用程序,任何人都可以帮助我从这个库中上传图片。谢谢

1 个答案:

答案 0 :(得分:0)

我建议将图像上传到第三方服务器,然后通过XMPP发送URL。

以下是使用Backendless提供20GB免费文件上传的示例。您还必须按照Backendless文档安装他们的SDK并设置API密钥:

[backendless.fileService.permissions grantForAllRoles:fileName operation:FILE_READ];
[backendless.fileService saveFile:fileName content:file response:^(BackendlessFile * file) {

    // We have the url so we can send the message

} error:^(Fault * fault) {
}];

或使用AFNetworking的PHP服务器:

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:_url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:file name:@"upfile" fileName:name mimeType:mimeType];
} error:nil];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
              uploadTaskWithStreamedRequest:request
              progress:^(NSProgress * _Nonnull uploadProgress) {
                  // This is not called back on the main queue.
                  // You are responsible for dispatching to the main queue for UI updates
                  dispatch_async(dispatch_get_main_queue(), ^{
                      //Update the progress view                           
                      [progressView setProgress:uploadProgress.fractionCompleted];
                  });
              }
              completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                  if (error) {
                  } else {
                      NSString *filePath = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

                      // Send message here

                  }
              }];

[uploadTask resume];

然后,您可以将图像和缩略图URL以及图像尺寸添加到XMPP消息中:

    [xmppMessage addBody:imageURL];
    [xmppMessage addAttributeWithName:bMessageImageURL stringValue:imageURL];
    [xmppMessage addAttributeWithName:bMessageThumbnailURL stringValue:thumbnailURL];
    [xmppMessage addAttributeWithName:bMessageImageWidth floatValue:width.floatValue];
    [xmppMessage addAttributeWithName:bMessageImageHeight floatValue:height.floatValue];

我建议将正文设置为imageURL,以便可以从标准XMPP客户端打开图像。

收到邮件后,您可以按如下方式再次访问详细信息:

    NSString * imageURL = [[message attributeForName:bMessageImageURL] stringValue];
    NSString * thumbnailURL = [[message attributeForName:bMessageThumbnailURL] stringValue];
    float width = [[[message attributeForName:bMessageImageWidth] stringValue] floatValue];
    float height = [[[message attributeForName:bMessageImageHeight] stringValue] floatValue];

然后,您可以使用SDWebImage显示图像。

我还推荐这个ChatSDK,这是iOS的完整前端消息传递界面。它包含UITableView单元格以显示图像消息。我用它来构建一个使用XMPPFramework的XMPP信使。