前言
为什么要把录像和拍照放在一起说呢?因为它们有很多相似的地方,那么我来简单的说一下吧!
1、初始化对象
UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
2、区分选择使用摄像头还是相册,默认的是选择相册,所以当选择摄像头设备的时候,需要设置它的sourceType属性:
//摄像头:
UIImagePickerControllerSourceTypeCamera,
//相册:
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
3、首先要导入框架<MobileCoreServices/MobileCoreServices.h>,因为需要的内容不是OC里面的字符串类型,需要强制转换。然后,定义一个BOOL类型的对象来判断拍照摄像的依据。最后,要选择媒体类型,系统默认的媒体类型是kuTTypeImage,所以在拍照的时候不设置媒体类型不会崩溃,但是在录制视频的时候,需要选择kUTTypeMovie,否则就会崩溃。
4、挂上代理,delegate包含两个代理协议UINavigationControllerDelegate和UIImagePickerControllerDelegate。
5、区分是拍照还是录像,系统默认的是拍照,所以在录像是,需要设置它的cameraCaptureMode属性。
//录像 UIImagePickerControllerCameraCaptureModeVideo
// 拍照 UIImagePickerControllerCameraCaptureModePhoto
if (isMovie == YES) {
pickerController.mediaTypes = @[(NSString *)kUTTypeMovie];
pickerController.delegate = self;
pickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
}
另外,还有一些其它的属性:
区分 前后摄像头,用的是 cameraDevice属性
后置摄像头:UIImagePickerControllerCameraDeviceRear,
前置摄像头:UIImagePickerControllerCameraDeviceFront
设置 是否显示 控制控件
showsCameraControls 默认显示控制控件
设置拍照
takePicture
录像
startVideoCapture
stopVideoCapture
设置视频清晰度videoQuality
UIImagePickerControllerQualityTypeHigh = 0, // 高清
UIImagePickerControllerQualityTypeMedium = 1, // medium quality,
UIImagePickerControllerQualityTypeLow = 2, // lowest quality,
UIImagePickerControllerQualityType 640x480 // VGA quality
UIImagePickerControllerQualityTypeIFrame 1280x720
UIImagePickerControllerQualityTypeIFrame 960x540
设置视频最大录像时间 videoMaximumDuration
默认10分钟
设置闪光 cameraFlashMode
UIImagePickerControllerCameraFlashModeOff = -1,//关闭
UIImagePickerControllerCameraFlashModeAuto = 0,//自动 默认
UIImagePickerControllerCameraFlashModeOn = 1//开启
设置调用摄像头视图页面的 覆盖视图 cameraOverlayView
设置拍照页面的形态 cameraViewTransform
用到的两个代理方法:
// 采集完成之后去调用,不区分拍照 摄像
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSLog(@"完成");
}
// 采集取消的时候调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"取消");
[self dismissViewControllerAnimated:YES completion:nil];
}
如何在采集完成代理方法内区分是拍照还是录像?代码如下:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
if ([info[UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeMovie]) {
NSLog(@"我在录像 录完了");
// 保存视频到相册
UISaveVideoAtPathToSavedPhotosAlbum(info[UIImagePickerControllerMediaType], self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
}
if ([info[UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeImage]) {
NSLog(@"我在拍照 拍完了");
UIImage *finishImage = info[UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(finishImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
[self dismissViewControllerAnimated:YES completion:nil];
}