向七牛上传视频和多张图片,视频可压缩

上传多张图片
-(void)uploadImages:(NSArray *)images atIndex:(NSInteger)index token:(NSString *)token uploadManager:(QNUploadManager *)uploadManager{
    //能拿到图片data的模型
    ACMediaModel *model = images[index];
    __block NSInteger imageIndex = index;
    NSData *data = model.uploadType;
    [uploadManager putData:data key:nil token:token
                  complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
                      if (info.isOK) {
                          ###//拼接下载链接(域名/resp[@"key"])
                          NSString *url = [NSString stringWithFormat:@"%@/%@", @"http://p9xk1q206.bkt.clouddn.com", resp[@"key"]];
                          //存储多张图片的链接数组
                          [self.mediaStrArr addObject:url];
                          imageIndex++;
                          if (imageIndex >= images.count) {
                              //我这边后台要求把多张图片的链接拼接成一个字符串,以逗号隔开
                              self.mediaStr = [self.mediaStrArr componentsJoinedByString:@","];
                              //调自己后台接口
                              [self sendForm];
                          }
                          [self uploadImages:images atIndex:imageIndex token:token uploadManager:uploadManager];
                      }
                  } option:nil];
}
视频压缩
//视频压缩传值
- (void)compressAndSendVideo:(NSString *)videoPath complete:(void(^)(BOOL isSuccess, NSData *newData))complete{
    
    __block NSData *data;
    //添加视频转码并压缩
    NSString *filena = [NSString stringWithFormat:@"%d.mp4",[self getRandomNumber:1 to:100]];
    //新路径不能存在文件 如果存在是不能压缩成功的
    NSString *sandBoxFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:filena];
    [[NSFileManager defaultManager] removeItemAtPath:sandBoxFilePath error:nil];
    
    AVURLAsset *ass = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:videoPath] options:nil];
    //压缩视频
    AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:ass presetName:AVAssetExportPreset640x480];
    exportSession.shouldOptimizeForNetworkUse = YES;
    //输出路径
    exportSession.outputURL = [NSURL fileURLWithPath:sandBoxFilePath];
    //输出格式
    exportSession.outputFileType = AVFileTypeMPEG4;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
        int exportStatus = exportSession.status;
        
        if (exportStatus == AVAssetExportSessionStatusCompleted) {
            //压缩成功
            data = [NSData dataWithContentsOfFile:sandBoxFilePath];
            complete(YES, data);
            [[NSFileManager defaultManager] removeItemAtPath:sandBoxFilePath error:nil];
        }else{
            //其他情况默认失败,发送原视频
            complete(NO, nil);
            [[NSFileManager defaultManager] removeItemAtPath:sandBoxFilePath error:nil];
        }
    }];
}

//获得一个随机整数,范围在[from,to],包括from,包括to
-(int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to-from + 1)));
}
压缩程度可调,如下图只是一部分
 AVAssetExportSession *exportSession= [[AVAssetExportSession alloc] initWithAsset:ass presetName:AVAssetExportPreset640x480];

上传视频

//上传视频
- (void)upLoadVedio:(NSArray *)vedios token:(NSString *)token UploadManager:(QNUploadManager *)uploadManager{
    //只有一个视频
    ACMediaModel *model = vedios[0];
    NSData *data = model.uploadType;
    [uploadManager putData:data key:nil token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        
        if (info.isOK) {
            ###这里可以做自己的处理
          //  self.mediaStr = [NSString stringWithFormat:@"%@/%@", @"http://p9xk1q206.bkt.clouddn.com", resp[@"key"]];
           // self.vedioKey = resp[@"key"];
           // [self getTranscodingIdWithKey:resp[@"key"]];
        }
        
    } option:nil];
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 哄两娃睡醒一觉了起来上厕所看到另一个房间老公拿着手机,问了在干嘛呢?老公赶紧关了手机说没干嘛,准备睡了。有那种被欺...
    叶青丁当妈阅读 304评论 1 4
  • 感觉抱着通过面试来发现自己的不足和不断成长的心态,面了好多公司,来做个面试总结吧。 面试知识点总结 遇到的面试,大...
    进击的前端阅读 620评论 2 8
  • 记住一句话:凡事能发送url请求的,都是客户端!Ajax就是基于这个原则! 而post也只是url请求中的一种请求...
    FKTX阅读 751评论 0 1
  • 细雨远山春已暮, 长空碧落卷云烟。 一声笛曲闻莺泪, 几缕青丝说燕咽。 梦里春情难可诉, 心中别恨岂能言。 何时相...
    断红尘阅读 652评论 0 1