压缩:
// 获取视频文件的url
NSURL *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
AVAsset * asset = [AVAsset assetWithURL:mediaURL];
//设置压缩质量
AVAssetExportSession * session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
//优化网络
session.shouldOptimizeForNetworkUse = YES;
NSString* path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"hello.mp4"];
[[NSFileManager defaultManager]removeItemAtPath:path error:nil];
//设置输出路径
session.outputURL = [NSURL fileURLWithPath:path];
//设置输出类型 这里可以更改输出的类型 具体可以看文档描述
session.outputFileType = AVFileTypeMPEG4;
[session exportAsynchronouslyWithCompletionHandler:^{
if (session.status == AVAssetExportSessionStatusCompleted) {
//压缩完成
dispatch_async(dispatch_get_main_queue(), ^{
NSURL * CompressURL = session.outputURL;
_videoImageView.image = [JKVideoAudioTool getThumImageForVideoUrl:mediaURL atTime:0];
// NSString * str = [NSString stringWithContentsOfURL:mediaURL encoding:kCFStringEncodingUTF8 error:nil];
NSLog(@"*********** url :%@",CompressURL);
[self.dataSource removeAllObjects];
[self.dataSource addObject:CompressURL];
});
}
}];
}
上传:传data 设置格式
NSData *data = [NSData dataWithContentsOfURL:img];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.mp4", str];
[formData appendPartWithFileData:data name:photoKey fileName:fileName mimeType:@"multipart/form-data"];
网友评论