AFN上传视频

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
    
    NSString *urlString =UpLoadUrl;

    //    //post请求
    
    [manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat   = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.mp4", str];
        /*
         此方法参数
         1. 要上传的[二进制数据]
         2. 我这里的imgFile是对应后台给你url里面的图片参数,别瞎带。
         3. 要保存在服务器上的[文件名]
         4. 上传文件的[mimeType]
         */
        NSURL * videoUrl = [NSURL URLWithString:path];
//        NSLog(@"视频地址是====%@",videoUrl);
        [formData appendPartWithFileURL:videoUrl name:@"mfile" fileName:fileName mimeType:@"video/mpeg4" error:(nil)];

    } progress:^(NSProgress * _Nonnull uploadProgress) {
        float progress =  1.0 * uploadProgress.completedUnitCount/uploadProgress.totalUnitCount;
//        NSLog(@"上传视频进度%f",progress);
        //找到message大数组中对应数据以更改,加入上传进度
        NSString *who;
        int which = 0;
        NSString *file1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"message.count"];
        if (![[NSFileManager defaultManager] fileExistsAtPath:file1]) {
        }else{
            
            NSMutableDictionary *countDic = [[NSMutableDictionary alloc]initWithContentsOfFile:file1];
            NSString* time = [NSString stringWithFormat:@"%ld",timeStamp];
            NSMutableDictionary *valueDic = [countDic objectForKey: time];
            who = [valueDic objectForKey:@"to"];
            which = [[valueDic objectForKey:@"count"] intValue];
            NSData *userData = [[NSUserDefaults standardUserDefaults] objectForKey:K_USER_USERMESSAGE];
            MxmUserMessage *mxmUserModel = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
            NSString *my = mxmUserModel.userName;
            
            NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"message.data"];
            if (![[NSFileManager defaultManager] fileExistsAtPath:file]) {
            }else{
                NSMutableDictionary *aDic = [[NSMutableDictionary alloc]initWithContentsOfFile:file];
                NSMutableArray *dataArr = [aDic objectForKey:my];
                
                for (int i = 0; i<dataArr.count; i++) {
                    NSMutableArray *smallArr = [dataArr objectAtIndex:i];
                    //下面三句只是为了拿到samllArr中每个字的key(每个字典的key都是相同的)
                    NSMutableDictionary *msgDic = [smallArr objectAtIndex:0];
                    NSArray *keyArr = msgDic.allKeys;
                    NSString *key = keyArr[0];
                    
                    if ([key isEqualToString:who]) {
                        NSMutableDictionary *msgDic1 = [smallArr objectAtIndex:which -1];
                        NSMutableDictionary *keyDic = [msgDic1 objectForKey:key];
                        
                        [keyDic setObject:@"1" forKey:@"isSender"];
                        [keyDic setObject:[NSString  stringWithFormat:@"%f",progress] forKey:@"uploadProgress"];
                        [msgDic1 setObject:keyDic forKey:key];
                        [smallArr replaceObjectAtIndex:which -1 withObject:msgDic1];
                        [dataArr replaceObjectAtIndex:i withObject:smallArr];
                        [aDic setObject:dataArr forKey:my];
                        [aDic writeToFile:file atomically:YES];
//                        NSLog(@"加入了上传进度-- 第%d句,时间是%@",which,time);
                        dispatch_async(dispatch_get_main_queue(), ^{
                            
                            [[NSNotificationCenter defaultCenter]postNotificationName:@"loadDataSource" object:nil];
                        });
                    }
                }
            }
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *mediaID = [responseObject objectForKey:@"mediaid"];
        NSString* urlStr= [NSString stringWithFormat:@"%@/%@",DownLoadUrl,mediaID];
        if (your.length <36) {
            [vc sendMessageWithContent:urlStr localUrl:path type:type realTime:timeStamp];
        }else{
            JFGroupChatController *groupVC = [[JFGroupChatController alloc]init];
            [groupVC sendMessageWithContent:urlStr localUrl:path type:type realTime:timeStamp];
        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败:%@",error);
    }];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 用实现的功能: 利用UIImagePickerController拍摄视频、图片,并从图库中选择图片或者视频上传 ...
    7dfa9c18c1d1阅读 8,519评论 0 5
  • 1.获取视频路径 2.创建压缩视频类.h文件 import <Foundation/Foundation.h> i...
    赤焰军少帅林殊阅读 4,136评论 0 0
  • 我们在项目中有时会碰到视频相关的需求,一般的可以分为几种情况: 1. 简单的视频开发,对界面无要求,可直接使用系统...
    纯情_小火鸡阅读 21,222评论 2 103
  • 史临平 人,生而在世,不断相遇,不同经历。 自打呱呱落地的那一瞬起,遇父母,接受他们倾尽心血的扶养,便要知感恩,懂...
    Snailqiao阅读 2,755评论 0 0
  • 1.物理错题 2.物理反思 3.默《师说》、《赤壁赋》 4.化学白本专题 5.化学卷子两份 6.生物世纪金榜2.1...
    Katherine_Zmj阅读 1,652评论 0 0

友情链接更多精彩内容