发送图片、文件到指定服务器


            
2016-12-28 11:12 | iOS

回答:

融云SDK默认将图片文件消息上传默认的文件服务器,如果需要将您自己的服务器,那么可以采用下面的方案

1,调用RCIMClient的接口

- (RCMessage *)sendMediaMessage:(RCConversationType)conversationType

                       targetId:(NSString *)targetId

                        content:(RCMessageContent *)content

                    pushContent:(NSString *)pushContent

                       pushData:(NSString *)pushData

                  uploadPrepare:(void (^)(RCUploadMediaStatusListener *uploadListener))uploadPrepareBlock

                       progress:(void (^)(int progress, long messageId))progressBlock

                        success:(void (^)(long messageId))successBlock

                          error:(void (^)(RCErrorCode errorCode, long messageId))errorBlock

                         cancel:(void (^)(long))cancelBlock

在uploadPrepareBlock中写代码上传您的图片、文件到您的服务器,uploadListener具体的实现可以参考下面代码,具体实现是根据上传的进度与过程给uploadListener的相关block赋值;可以在progressBlock中根据progress的值来更新UI

2、调用RCConversationViewController的接口

- (void)sendMediaMessage:(RCMessageContent *)messageContent

             pushContent:(NSString *)pushContent

               appUpload:(BOOL)appUpload;

同时需要实现下面的接口

- (void)uploadMedia:(RCMessage *)message

     uploadListener:(RCUploadMediaStatusListener *)uploadListener;

在该方法中给上传到您服务器的代码,并更新uploadListener中相关block的状态

可以参考下面代码:

RCImageMessage *imgMsg = [RCImageMessage messageWithImage:image];

[self sendMediaMessage:imgMsg pushContent:nil appUpload:YES];


- (void)uploadMedia:(RCMessage *)message

     uploadListener:(RCUploadMediaStatusListener *)uploadListener {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{   

         //这里是模仿图片、文件上传的过程

        BOOL isSuccess = NO;

        int i = 0;

        for (i = 0; i < 100; i++) {

            uploadListener.updateBlock(i);

            [NSThread sleepForTimeInterval:0.2];

            isSuccess = YES;

        }

        if(isSuccess){//如果上传成功

            uploadListener.successBlock(@"http://www.rongcloud.cn/images/newVersion/bannerInner.png?0717");

        }else {//如果上传失败

            uploadListener.errorBlock(-1);

        }

    });

}


您认为此回答对您有帮助?

共有 0 位开发者认为此问题有帮助

我对此仍有疑问!继续追问