示例代码:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 获取视频
// 对该视频进行压缩
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// 创建图片控制器
UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
/** 设置控制器类型
* UIImagePickerControllerSourceTypePhotoLibrary, 相簿
UIImagePickerControllerSourceTypeCamera, 相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum 照片
*/
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
/* 设置媒体类型 : 如果想要获取视频文件,必须要设置媒体类型,否则弹出的控制器下默认不显示视频文件
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
*/
NSArray *mediaTypeArr = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
pickerController.mediaTypes = mediaTypeArr;
// modal展示
[self presentViewController:pickerController animated:YES completion:nil];
// 设置代理
pickerController.delegate = self;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
/* NSLog(@"%@",info); Log信息:
2016-07-27 20:19:40.828 视频压缩[3828:3690420] {
UIImagePickerControllerMediaType = "public.movie";
UIImagePickerControllerMediaURL = "file:///private/var/mobile/Containers/Data/Application/6D369842-32F4-455C-9F40-8B55D44263C8/tmp/trim.C17F3DC5-CF34-4F72-B319-CD28B957431B.MOV";
UIImagePickerControllerReferenceURL = "assets-library://asset/asset.MOV?id=9603CD2B-5692-417F-BC8F-6CA0C780113C&ext=MOV";
}
*/
// 进行资源转出
// 获取资源对象
AVAsset *asset = [AVAsset assetWithURL:info[UIImagePickerControllerMediaURL]];
// 创建资源转出会话 参数1: 转出的资源文件 参数2: 压缩质量
/*
AVAssetExportPresetLowQuality 低质量
AVAssetExportPresetMediumQuality 中质量
AVAssetExportPresetHighestQuality 高质量
*/
AVAssetExportSession *session = [AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPresetLowQuality];
// 设置转出文件名
/* 不设置转出文件名,会崩溃报错:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'outputURL cannot be NULL'
同样,不是设置转储文件类型,也会报错:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'outputFileType cannot be NULL'
*/
NSURL *outPutURL = [[NSURL fileURLWithPath:NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject] URLByAppendingPathComponent:@"转出视频.mp4"];
session.outputURL = outPutURL;
// 设置转出文件类型
// 通过该方法可以获取会话转出的可支持类型
[session determineCompatibleFileTypesWithCompletionHandler:^(NSArray<NSString *> * _Nonnull compatibleFileTypes) {
NSLog(@"%@",compatibleFileTypes);
/**
* Log信息:
2016-07-27 20:45:15.584 视频压缩[3873:3699090] (
"com.apple.quicktime-movie",
"public.mpeg-4"
)
*/
}];
session.outputFileType = @"public.mpeg-4";
// 进行转出操作
[session exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"完成视频压缩");
}];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
通过Xcode可以获取到真机的应用沙盒,就可以找到压缩后的视频