iOS 视频选择压缩

//原理,还是调用UIImagePickerController控制器,设置Type为视频

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

//打开控制器

UIImagePickerController *picker=[[UIImagePickerController alloc]init];

//picker.sourceType=UIImagePickerControllerSourceTypeCamera;//设置摄像头类型 摄像头

//picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;//打开相册,获取资源,视频和图片用户拍摄的照片可以删除,

//picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;//打开相册库,用户与电脑同步的,不能删除

picker.delegate=self;

//设置媒体类型

picker.mediaTypes=[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

//显示控制器

[self presentViewController:picker animated:YES completion:nil];

}

#pragma mark UIImagePickerControllerDelegate

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

//如果是视频返回的是URL

NSURL*url=info[UIImagePickerControllerMediaURL];

//使用媒体工具 压缩

[self compressVideo:url];

//关闭控制器

[self dismissViewControllerAnimated:YES completion:nil];

}

//压缩

-(void)compressVideo:(NSURL*)url

{

//使用媒体工具(AVFoundation框架下的)

//Asset 资源 可以是图片音频视频

AVAsset *asset=[AVAsset assetWithURL:url];

//设置压缩的格式

AVAssetExportSession *session=[AVAssetExportSession exportSessionWithAsset:asset presetName:AVAssetExportPresetMediumQuality];//mediumquality:中等质量

//设置导出路径

NSString *path=[NSTemporaryDirectory() stringByAppendingPathComponent:@"needAV.mov"];

//创建文件管理类 导出失败,删除已经导出的

NSFileManager *manager=[[NSFileManager alloc]init];

//删除已经存在的

[manager removeItemAtPath:path error:NULL];

//设置到处路径

session.outputURL=[NSURL fileURLWithPath:path];

//设置输出文件的类型

session.outputFileType=AVFileTypeQuickTimeMovie;

//开辟子线程处理耗时操作

[session exportAsynchronouslyWithCompletionHandler:^{

NSLog(@"导出完成!路径:%@",path);

}];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

@end

//参考资料 http://www.cnblogs.com/wanghuaijun/p/5575255.html。

上一篇:WPF之鼠标滑动切换图片


下一篇:iOS文档注释