//判断设备是否支持录像功能
- (BOOL)isAvailableVideoRecording {
if ([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) {
NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:(UIImagePickerControllerSourceTypeCamera)];//支持的媒体类型
if ([mediaTypes containsObject:(NSString *)kUTTypeMovie]) {
return YES;
}
}
return NO;
}
//创建imagePicker
- (UIImagePickerController *)picker {
if (!_picker && [self isAvailableVideoRecording]) {
_picker = [[UIImagePickerController alloc] init];
_picker.sourceType = UIImagePickerControllerSourceTypeCamera;
_picker.mediaTypes = @[(NSString *)kUTTypeMovie];
_picker.delegate = self;
_picker.videoQuality = UIImagePickerControllerQualityTypeHigh;//质量
}
return _picker;
}
//设置前置摄像头
- (BOOL)setFrontFacingCameraOnImagePicker {
if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
[self.picker setCameraDevice:(UIImagePickerControllerCameraDeviceFront)];
return YES;
}
return NO;
}
//设置自定义UI
- (void)setCustomUIOnImagePicker {
UIView *customView = [[UIView alloc] init];
self.picker.showsCameraControls = NO;//不显示默认UI
self.picker.cameraOverlayView = customView;
//- (BOOL)startVideoCapture NS_AVAILABLE_IOS(4_0);
//- (void)stopVideoCapture NS_AVAILABLE_IOS(4_0);
}
#pragma mark ------ UIImagePickerControllerDelegate ---------
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
NSURL *videoUrl = [info objectForKey:UIImagePickerControllerMediaURL];
//保存到相册
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:videoUrl]) {
[library writeVideoAtPathToSavedPhotosAlbum:videoUrl completionBlock:nil];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}