一般来说,我们应该都会用到这两个功能所以下面就简单的描述下,记录下了,其实我们如果想要去录制视频的话,其实和选择照片是很像的,如下所示,注意的时候下面用到的kUTTypeMovie默认是不识别的,所以我们需要去导入一个框架#import <MobileCoreServices/MobileCoreServices.h>,还有就是我们需要设置mediaTypes为kUTTypeMovie,关于它的解释,我们其实点开kUTTypeMovie里面就能看到下面的解释
//1、判断是否可用if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){return;}//2、创建图像选择器UIImagePickerController * picker = [UIImagePickerController new];//3、设置类型picker.sourceType = UIImagePickerControllerSourceTypeCamera;//4、设置媒体类型,我们需要用NSString去强转下picker.mediaTypes = @[(NSString *)kUTTypeMovie];//5、设置代理picker.delegate = self;//设置相机的检测模式picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;//设置视频的质量picker.videoQuality = UIImagePickerControllerQualityTypeMedium;//6.模态弹出[self presentViewController:picker animated:YES completion:nil];
我们可以在下面的这个代理方法中拿到我们拍摄的视频,我们可以对他进行读取和保存操作,通过打印info,我们可以得到下面的这两个东西,其中第一个key所对应的value就是kUTTypeMovie的NSString的形式。如下图所示
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{NSLog(@"%@",info);//1、获取媒体的类型 public.movieNSString * mediaType = info[UIImagePickerControllerMediaType];//获取url,然后下面进行播放和保存id url = info[UIImagePickerControllerMediaURL];//判断是否是媒体类型if([mediaType isEqualToString:(NSString *)kUTTypeMovie]){//先去判断控制器是否存在,如果不存在就是创建if(self.playerVC==nil){self.playerVC = [AVPlayerViewController new];self.playerVC.view.frame = self.view.bounds;//CGRectMake(40, 40, 200, 200);[self.view addSubview:self.playerVC.view];[self.playerVC.view addSubview:self.backButton];}//先去用url去创建一个AVPlayer对象AVPlayer * player = [AVPlayer playerWithURL:url];self.playerVC.player = player;}//判断是不是相机类型if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){NSError * error;//去保存视频[[PHPhotoLibrary sharedPhotoLibrary]performChangesAndWait:^{[PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];} error:&error];}[picker dismissViewControllerAnimated:YES completion:nil];
}
需要注意的是这里我们如果不是去通过modal出来AVPlayerViewController,而是直接把AVPlayerViewController的view加到某个视图上面的,并且是设置全屏显示的那么就不会有Done,如果不是全屏显示就会有Done。如果没有Done的话,我们可以自己去添加一个返回按钮。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{NSLog(@"%@",info);//1、获取媒体的类型 public.movieNSString * mediaType = info[UIImagePickerControllerMediaType];//获取url,然后下面进行播放和保存id url = info[UIImagePickerControllerMediaURL];//判断是否是媒体类型if([mediaType isEqualToString:(NSString *)kUTTypeMovie]){//先去判断控制器是否存在,如果不存在就是创建if(self.playerVC==nil){self.playerVC = [AVPlayerViewController new];self.playerVC.view.frame = CGRectMake(40, 40, 200, 200);[self.view addSubview:self.playerVC.view];[self.playerVC.view addSubview:self.backButton];}//先去用url去创建一个AVPlayer对象AVPlayer * player = [AVPlayer playerWithURL:url];self.playerVC.player = player;}//判断是不是相机类型if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){NSError * error;//去保存视频[[PHPhotoLibrary sharedPhotoLibrary]performChangesAndWait:^{[PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];} error:&error];}[picker dismissViewControllerAnimated:YES completion:nil];
}