简体   繁体   中英

How we can upload image or photos to ejabberd XMPP web server?

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

I would recommend uploading images to a third party server and then sending the URL via XMPP.

Here's an example using Backendless which offer 20GB of free file uploads. You would have to also follow the Backendless docs to install their SDK and setup the API keys:

[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) {
}];

Or to a PHP server using AFNetworking :

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];

Then you can add the image and thumbnail URLs and the image dimensions to the XMPP message:

    [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];

I'd recommend setting the body to the imageURL so that the image could be opened from a standard XMPP client.

When the message is received, you can access the details again as follows:

    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];

You could then display the image using SDWebImage .

I'd also recommend this ChatSDK which is a full front end messaging interface for iOS. It contains UITableView cells to display image messages. I used it to build an XMPP messenger using XMPPFramework .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM