#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
/*
UIImagePickerControllerSourceTypePhotoLibrary, 照片库 备份
UIImagePickerControllerSourceTypeCamera, 相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum 相片库自己拍的
*/
NSLog(@"%@",[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum]);
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
[self presentViewController:picker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//获取视频地址
NSURL *movieUrl = info[UIImagePickerControllerMediaURL];
//压缩出来
[self compressMovie:movieUrl];
}
//压缩方法
- (void)compressMovie:(NSURL *)url
{
//1.创建一个素材(视频的url生成)对象
AVAsset *asset = [AVAsset assetWithURL:url];
//2.创建导出(低质量的导出就是压缩)回话
AVAssetExportSession *session = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetMediumQuality];
//AVAssetExportPresetLowQuality 3g 环境
//AVAssetExportPresetMediumQuality wifi 环境
//AVAssetExportPresetHighestQuality 原来的质量没有必要压缩
//保存的路
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"123.mov"];
//删除之前可能存在的文件
//如果不存在 这个文件 这个方法就啥都不做
[[NSFileManager defaultManager]removeItemAtPath:path error:nil];
//设置输出文件的url
session.outputURL = [NSURL fileURLWithPath:path];
//设置输出文件的类型
session.outputFileType = AVFileTypeQuickTimeMovie;
[session exportAsynchronouslyWithCompletionHandler:^{
NSLog(@"导出完成! %@", [NSThread currentThread]);
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
@end
网友评论