上传多张图片
-(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];
}
网友评论