//视频压缩的原理就是以低质量的导出
- (void)viewDidLoad {
[superviewDidLoad];
//从相册中选择视频出来
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];
//设置素材的来源
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.picker = picker;
/*
UIImagePickerControllerSourceTypePhotoLibrary, 相片库
UIImagePickerControllerSourceTypeCamera, 相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum 相册
*/
NSLog(@"%@",[UIImagePickerControlleravailableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary]);
self.picker.delegate =self;
//设置多媒体类型 图片 音频视频
self.picker.mediaTypes = [UIImagePickerControlleravailableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
// NSLog(@"%@",info);
// UIImage *image = info[UIImagePickerControllerOriginalImage];
// self.imageView.image = image;
[self.pickerdismissViewControllerAnimated:YEScompletion:nil];
//获取视频的url
NSURL *url = info[UIImagePickerControllerMediaURL];
//视频压缩
//8M 200KB 应用场景 对像素要求不高但是需要看到视频的场景
// 小视频 网络传输
//压缩原理: 就是以低质量的导出
AVAsset *asset = [AVAssetassetWithURL:url];
//参数1是素材对象 参数2 压缩的质量
AVAssetExportSession *session = [[AVAssetExportSessionalloc]initWithAsset:assetpresetName:AVAssetExportPresetMediumQuality];
//建议 : AVAssetExportPresetHighestQuality 不压缩 AVAssetExportPresetLowQuality低质量
//AVAssetExportPresetMediumQuality 中间
//路径 放在什么地方?
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"hello.m4v"];
//删除之前存在的文件 如果没有就设么都不做
[[NSFileManagerdefaultManager]removeItemAtPath:patherror:nil];
session.outputURL = [NSURLfileURLWithPath:path];
//文件格式
// self.session = session;
NSLog(@"%@",session.supportedFileTypes);
session.outputFileType =AVFileTypeQuickTimeMovie;
// NSLog(@"%@",NSHomeDirectory());
//导出
[session exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"导出完成!!!");
}];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[selfpresentViewController:self.pickeranimated:YEScompletion:nil];
}