一,//相册
- (IBAction)pickerImage:(id)sender {
//打开相册
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];
//设置照片源
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; |
//参数设置
picker.allowsEditing = YES;
//设置代理
picker.delegate = self;
//弹出控制器
[self.navigationControllerpresentViewController:picker animated:YEScompletion:nil];
}
//选中了图片
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
NSLog(@"%@",info);
UIImage *image = info[UIImagePickerControllerEditedImage];
NSData *imageData = UIImageJPEGRepresentation(image, 0.2);//对图像做了压缩,0.2是失真度
//dismiss控制器
[selfdismissViewControllerAnimated:YEScompletion:nil];
}
//取消图片选择控制器
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[selfdismissViewControllerAnimated:YEScompletion:nil];
}
- (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];
}