【问题标题】:How we can upload image or photos to ejabberd XMPP web server?我们如何将图像或照片上传到 ejabberd XMPP Web 服务器?
【发布时间】:2016-12-27 14:48:28
【问题描述】:

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

【问题讨论】:

    标签: ios swift xmpp ejabberd xmppframework


    【解决方案1】:

    我建议将图像上传到第三方服务器,然后通过 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 Messenger。

    【讨论】:

    • 你的回答可能是对的,但问题被标记为“swift”,而不是“objective-c”,所以这有点离题...:/
    • 啊,是的,我没有看到,因为它没有在标题中指定。我会尽量提供 Swift 版本。
    • 语言应该从不出现在标题中,实际上 - 它的位置在标签中。总是。 // 谢谢。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    相关资源
    最近更新 更多