1、权限配置
相机权限 Privacy - Camera Usage Description App想要访问您手机的系统相机,是否允许?
麦克风权限 Privacy - Microphone Usage Description App想要访问您手机的麦克风,是否允许?
录制音视频需要以上权限
2、引入头文件:里面定义了kUTTypeImage、kUTTypeMovie等,不引入会报错
#import <MobileCoreServices/MobileCoreServices.h>
3、打开系统相机录制视频的方法
- (void)openCamera {
UIImagePickerController *pickerCon = [[UIImagePickerController alloc]init];
pickerCon.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerCon.mediaTypes = @[(NSString *)kUTTypeMovie];//设定相机为视频
pickerCon.cameraDevice = UIImagePickerControllerCameraDeviceRear;//设置相机后摄像头
pickerCon.videoMaximumDuration = 10;//最长拍摄时间
pickerCon.videoQuality = UIImagePickerControllerQualityTypeHigh;//拍摄质量
pickerCon.allowsEditing = NO;
pickerCon.delegate = self;
[self presentViewController:pickerCon animated:YES completion:nil];
}
4、回调
#pragma mark - UIImagePickerControllerDelegate
//selected
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//此处需要先dismiss掉picker,然后再present出alert,佛否则alert显示会出bug
[picker dismissViewControllerAnimated:YES completion:nil];
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
//如果是拍照
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
}
//如果是录制视频
else if([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
NSLog(@"*******info*******%@",info);
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
NSString *videoPath = url.path;
//保存到相册
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoPath)) {
NSLog(@"保存成功");
} else {
NSLog(@"保存失败");
}
}
}
}
//cancel
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}