#import "ViewController.h"#import@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // 视频压缩 就是视频以低质量导出 // 1.需要视频 // 2.选择质量 // 3.视频的名称 // 4.保存的路径 // 5.视频的类型}- (IBAction)btnClicker:(id)sender{ // 1.判断相册是否可用 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { NSLog(@"相册不可用"); return; } // 2.创建相册选择控制器 UIImagePickerController *pickerC = [[UIImagePickerController alloc] init]; // 3.设置来源 pickerC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // 4.设置代理 pickerC.delegate = self; // 5.设置可以选择的类型 pickerC.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum]; // 5.弹出控制器 [self presentViewController:pickerC animated:YES completion:nil];}- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
NSLog(@"选择了照片");
NSLog(@"%@",info);
// UIImagePickerControllerMediaURL
// 获取视频url
NSURL *url = info[UIImagePickerControllerMediaURL];
[self exportUrl:url];
// 销毁控制器
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)exportUrl:(NSURL *)url
{
// 2.选择质量
// 3.视频的名称
// 4.保存的路径
// 5.视频的类型
// 获取视频资源
AVAsset *asset = [AVAsset assetWithURL:url];
// 创建导入会话
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
// 导出的路径
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"123.mov"];
NSURL *fileUrl = [NSURL fileURLWithPath:path];
exportSession.outputURL = fileUrl;
NSLog(@"%@",exportSession.supportedFileTypes);
// 导出的视频类型
exportSession.outputFileType = @"com.apple.quicktime-movie";
// 开始导出视频
[exportSession exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"导出成功");
}];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"取消");
// 销毁控制器
[picker dismissViewControllerAnimated:YES completion:nil];
}
@end