一 UIImagePickerController简介和属性介绍。
UIImagePickerController继承于UINavigationController。UIImagePickerController可以用来选择照片,它还可以用来拍照和录制视频。首先看一下这个类常用的属性和方法:
属性 | 说明 |
@property(nonatomic) UIImagePickerControllerSourceType sourceType |
拾取源类型,sourceType是枚举类型:
UIImagePickerControllerSourceTypePhotoLibrary:照片库 ,默认值 UIImagePickerControllerSourceTypeCamera:摄像头 UIImagePickerControllerSourceTypeSavedPhotosAlbum:相簿 |
@property(nonatomic,copy) NSArray *mediaTypes | 媒体类型,默认情况下此数组包含kUTTypeImage,所以拍照时可以不用设置;但是当要录像的时候必须设置,可以设置为kUTTypeVideo(视频,但不带声音)或者kUTTypeMovie(视频并带有声音) |
@property(nonatomic) NSTimeInterval videoMaximumDuration | 视频最大录制时长,默认为10 s |
@property(nonatomic) UIImagePickerControllerQualityType videoQuality |
视频质量,枚举类型:
UIImagePickerControllerQualityTypeHigh:高清质量 UIImagePickerControllerQualityTypeMedium:中等质量,适合WiFi传输 UIImagePickerControllerQualityTypeLow:低质量,适合蜂窝网传输 UIImagePickerControllerQualityType640x480:640*480 UIImagePickerControllerQualityTypeIFrame1280x720:1280*720 UIImagePickerControllerQualityTypeIFrame960x540:960*540 |
@property(nonatomic) BOOL showsCameraControls | 是否显示摄像头控制面板,默认为YES |
@property(nonatomic,retain) UIView *cameraOverlayView | 摄像头上覆盖的视图,可用通过这个视频来自定义拍照或录像界面 |
@property(nonatomic) CGAffineTransform cameraViewTransform | 摄像头形变 |
@property(nonatomic) UIImagePickerControllerCameraCaptureMode cameraCaptureMode |
摄像头捕获模式,捕获模式是枚举类型:
UIImagePickerControllerCameraCaptureModePhoto:拍照模式 UIImagePickerControllerCameraCaptureModeVideo:视频录制模式 |
@property(nonatomic) UIImagePickerControllerCameraDevice cameraDevice |
摄像头设备,cameraDevice是枚举类型:
UIImagePickerControllerCameraDeviceRear:前置摄像头 UIImagePickerControllerCameraDeviceFront:后置摄像头 |
@property(nonatomic) UIImagePickerControllerCameraFlashMode cameraFlashMode |
闪光灯模式,枚举类型:
UIImagePickerControllerCameraFlashModeOff:关闭闪光灯 UIImagePickerControllerCameraFlashModeAuto:闪光灯自动 UIImagePickerControllerCameraFlashModeOn:打开闪光灯 |
类方法 | 说明 |
+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType |
指定的源类型是否可用,sourceType是枚举类型:
UIImagePickerControllerSourceTypePhotoLibrary:照片库 UIImagePickerControllerSourceTypeCamera:摄像头 UIImagePickerControllerSourceTypeSavedPhotosAlbum:相簿 |
+ (NSArray *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType | 指定的源设备上可用的媒体类型,一般就是图片和视频 |
+ (BOOL)isCameraDeviceAvailable:(UIImagePickerControllerCameraDevice)cameraDevice |
指定的摄像头是否可用,cameraDevice是枚举类型:
UIImagePickerControllerCameraDeviceRear:前置摄像头 UIImagePickerControllerCameraDeviceFront:后置摄像头 |
+ (BOOL)isFlashAvailableForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice | 指定摄像头的闪光灯是否可用 |
+ (NSArray *)availableCaptureModesForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice |
获得指定摄像头上的可用捕获模式,捕获模式是枚举类型:
UIImagePickerControllerCameraCaptureModePhoto:拍照模式 UIImagePickerControllerCameraCaptureModeVideo:视频录制模式 |
对象方法 | 说明 |
- (void)takePicture | 编程方式拍照 |
- (BOOL)startVideoCapture | 编程方式录制视频 |
- (void)stopVideoCapture | 编程方式停止录制视频 |
代理方法 | 说明 |
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info | 媒体拾取完成 |
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker | 取消拾取 |
扩展方法(主要用于保存照片、视频到相簿) | 说明 |
UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo) | 保存照片到相簿 |
UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(NSString *videoPath) | 能否将视频保存到相簿 |
void UISaveVideoAtPathToSavedPhotosAlbum(NSString *videoPath, id completionTarget, SEL completionSelector, void *contextInfo) | 保存视频到相簿 |
二 使用UIImagePickerController拍照和视频录制
#import "ViewController.h" #import <MobileCoreServices/MobileCoreServices.h> #import <AVFoundation/AVFoundation.h> @interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *mImageview; @property (nonatomic,assign) BOOL isTakeVedio; @property (nonatomic,strong) UIImagePickerController *imagePicker; @property (nonatomic,strong) AVPlayer *player; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)takePic:(id)sender { _isTakeVedio = NO; [self showPicker]; } - (IBAction)takeVedio:(id)sender { _isTakeVedio = YES; [self showPicker]; } - (UIImagePickerController *)imagePicker { if (_imagePicker == nil) { _imagePicker = [[UIImagePickerController alloc] init]; _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; //设置来源为摄像头 _imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceRear; //设置使用的摄像头为:后置摄像头 if (self.isTakeVedio) { _imagePicker.mediaTypes = @[(NSString *)kUTTypeVideo]; //设置为视频模式-<span style="color: rgb(51, 51, 51); font-family: Georgia, 'Times New Roman', Times, sans-serif; font-size: 14px; line-height: 25px;">注意媒体类型定义在MobileCoreServices.framework中</span> _imagePicker.videoQuality = UIImagePickerControllerQualityTypeIFrame1280x720; //设置视频质量 _imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo; //设置摄像头模式为录制视频 } else{ _imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto; //设置摄像头模式为拍照 } } return _imagePicker; } - (void)showPicker { [self presentViewController:self.imagePicker animated:YES completion:nil]; } #pragma mark- #pragma mark - UIImagePickerControllerDelegate - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; if ([type isEqualToString:(NSString *)kUTTypeImage]) { //图片保存和展示 UIImage *image; if (picker.allowsEditing) { image = [info objectForKey:UIImagePickerControllerEditedImage]; //允许编辑,获取编辑过的图片 } else{ image = [info objectForKey:UIImagePickerControllerOriginalImage]; //不允许编辑,获取原图片 } self.mImageview.image = image; UIImageWriteToSavedPhotosAlbum(image,nil,nil, nil); } else if([type isEqualToString:(NSString *)kUTTypeVideo]){ //视频保存后 播放视频 NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL]; NSString *urlPath = [url path]; if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlPath)) { UISaveVideoAtPathToSavedPhotosAlbum(urlPath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil); } } } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { } #pragma mark- #pragma mark - share method //视频保存后的回调 - (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ if (error) { NSLog(@"保存视频过程中发生错误,错误信息:%@",error.localizedDescription); }else{ NSLog(@"视频保存成功."); //录制完之后自动播放 NSURL *url=[NSURL fileURLWithPath:videoPath]; _player=[AVPlayer playerWithURL:url]; AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:_player]; playerLayer.frame = self.mImageview.frame; [self.mImageview.layer addSublayer:playerLayer]; [_player play]; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。