2016-12-27 130 views

回答

0

我會建議上傳圖像到第三方服務器,然後發送URL通過XMPP。

下面是一個使用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) { 
}]; 

或一個PHP服務器使用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]; 

然後你就可以添加圖像和縮略圖網址和圖像尺寸的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,使圖像可以opene d來自標準的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信使。

+0

你的回答可能是正確的,但問題被標記爲「迅速」,而不是「objective-c」,所以這是相當偏離主題...:/ – Moritz

+0

啊,是啊,我沒有看到,因爲它沒有沒有在標題中指定。我會盡力提供一個Swift版本。 –

+0

語言應該*永遠不會*在標題中,實際上 - 它的位置在標籤中。總是。 // 謝謝。 :) – Moritz