1.先看一下它包含的属性以及方法
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:打开闪光灯
UIImagePickerController类方法 说明
+ (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:视频录制模式
UIImagePickerController对象方法说明
- (void)takePicture 编程方式拍照
- (BOOL)startVideoCapture 编程方式录制视频
- (void)stopVideoCapture 编程方式停止录制视频
UIImagePickerController代理方法说明
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 媒体拾取完成
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 取消拾取
UIImagePickerController扩展方法(主要用于保存照片、视频到相簿)说明
UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo) 保存照片到相簿
UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(NSString *videoPath) 能否将视频保存到相簿
void UISaveVideoAtPathToSavedPhotosAlbum(NSString *videoPath, id completionTarget, SEL completionSelector, void *contextInfo) 保存视频到相簿
2.使用步骤
老规矩,首先添加框架AVFoundation、MobileCoreServices,引入头文件#import <AVFoundation/AVFoundation.h>,#import <MobileCoreServices/MobileCoreServices.h>记得这里需要遵循协议UIImagePickerControllerDelegate。
创建UIImagePickerController对象
UIImagePickerController *imagePicker=[[UIImagePickerController alloc]init];
imagePicker.delegate=self;//设置代理,检测操作
imagePicker.allowsEditing=YES/NO;//是否允许编辑
指定拾取源类型:拍照(相片库或者是相薄);录视频(摄像头)
imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;//录视频
imagePicker.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum|UIImagePickerControllerSourceTypePhotoLibrary;//拍照
指定使用的摄像头(前置摄像头或是后置摄像头)
imagePicker.cameraDevice=UIImagePickerControllerCameraDeviceRear/UIImagePickerControllerCameraDeviceFront;
设置媒体类型:拍照:KUTTypeImage(默认)录制视频选择:kUTTypeVideo(视频,但不带声音)kUTTypeMovie(视频并带有声音)
imagePicker.mediaTypes=@[(NSString *)KUTTypeImage];//拍照
imagePicker.mediaTypes=@[(NSString *)kUTTypeVideo];//视频,但不带声音
imagePicker.mediaTypes=@[(NSString *)kUTTypeMovie];//视频并带有声音
设置捕获模式拍照/录制视频
imagePicker.cameraCaptureMode=UIImagePickerControllerCameraCaptureModeVideo;//设置摄像头模式(拍照,录制视频)
imagePicker.cameraCaptureMode=UIImagePickerControllerCameraCaptureModePhoto;//拍照
其它设置可根据上面的属性按需要设置,接下来需要实现几个回调函数
//拍照或录制视频结束后调用
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];//用于判断媒体类型
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {//如果是拍照
UIImage *image;
//如果允许编辑则获得编辑后的照片,否则获取原始照片
if (imagePicker.allowsEditing) {
image=[info objectForKey:UIImagePickerControllerEditedImage];//获取编辑后的照片
}else{
image=[info objectForKey:UIImagePickerControllerOriginalImage];//获取原始照片
}
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);//保存图片到相簿
}else if([mediaType isEqualToString:(NSString *)kUTTypeMovie]){//如果是录制视频
NSURL *url=[info objectForKey:UIImagePickerControllerMediaURL];//视频路径
NSString *strUrl=[url path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(strUrl)) {
UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);//保存视频到相簿
}
}
}
//视频保存后的回调
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
error判断成功与否,做自己想要的操作
}