ios —— UIImagePickerController类

UIImagePickerController简述:

UIImagePickerController 类是获取选择图片和视频的用户接口,我们可以用UIImagePickerController选择我们所需要的图片和视频。

注:UIImagePickerController不能够任意定制,也不可以继承生成子类。

一、UIImagePickerController的属性:

1、sourceType

指定使用模式(照相机/相册/照片库)

enum {
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
};

2、BOOL showsCameraControls

设置拍照时的下方的工具栏是否显示,如果需要自定义拍摄界面,则可把该工具栏隐藏。

3、BOOL allowsEditing

设置当拍照完或在相册选完照片后,是否跳到编辑模式进行图片剪裁

showsCameraControls=Yes时才有效果。

4、cameraDevice

判断设备是否支持前置摄像头/后置摄像头

enum {
UIImagePickerControllerCameraDeviceRear,
UIImagePickerControllerCameraDeviceFront
};

5、cameraFlashMode

设置闪光灯模式

enum {
UIImagePickerControllerCameraFlashModeOff = -1,
UIImagePickerControllerCameraFlashModeAuto = 0,
UIImagePickerControllerCameraFlashModeOn = 1
};

6、mediaTypes

设置相机支持的类型,拍照和录像

  • (NSArray *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType
    一共有三个可选的代理方法UIImagePickerControllerDelegate
    – imagePickerController:didFinishPickingMediaWithInfo:
    – imagePickerControllerDidCancel:
    – imagePickerController:didFinishPickingImage:editingInfo:
  • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    info中包括选取的照片,视频的主要信息
    NSString *const UIImagePickerControllerMediaType; 选取的类型 public.image public.movie
    NSString *const UIImagePickerControllerOriginalImage; 修改前的UIImage object.
    NSString *const UIImagePickerControllerEditedImage; 修改后的UIImage object.
    NSString *const UIImagePickerControllerCropRect; 原始图片的尺寸NSValue object containing a CGRect data type
    NSString *const UIImagePickerControllerMediaURL; 视频在文件系统中 的 NSURL地址

7、cameraViewTransform

设置拍摄时屏幕的view的transform属性,可以实现旋转,缩放功能

enum{

CGAffineTransformMakeRotation;

CGAffineTransformMakeScale;

}

二、UIImagePickerController回调方法:

1、成功获得相片或视频后的回调

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
 2     //通过UIImagePickerControllerMediaType判断返回的是照片还是视频
 3     NSString* type = [info objectForKey:UIImagePickerControllerMediaType];
 4     //如果返回的type等于kUTTypeImage,代表返回的是照片,并且需要判断当前相机使用的sourcetype是拍照还是相册
 5     if ([type isEqualToString:(NSString*)kUTTypeImage]&&picker.sourceType==UIImagePickerControllerSourceTypeCamera) {
 6         //获取照片的原图
 7         UIImage* original = [info
 8 objectForKey:UIImagePickerControllerOriginalImage];
 9         //获取图片裁剪的图
10         UIImage* edit = [info objectForKey:UIImagePickerControllerEditedImage];
11         //获取图片裁剪后,剩下的图
12         UIImage* crop = [info objectForKey:UIImagePickerControllerCropRect];
13         //获取图片的url
14         NSURL* url = [info objectForKey:UIImagePickerControllerMediaURL];
15         //获取图片的metaData数据信息
16         NSDictionary* metaData = [info objectForKey:UIImagePickerControllerMediaMetadata];
17         //如果是拍照的照片,则需要手动保存到本地,系统不会自动保存拍照成功后的照片
18         UIImageWriteToSavedPhotosAlbum(edit, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
19     }else{
20     
21     }
22     //模态方式退出uiimagepickercontroller
23     [imagepicker dismissModalViewControllerAnimated:YES];

2、取消照相机的回调

  • (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
1 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
2     //模态方式退出UIImagePickerController
3     [imagePicker dismissModalViewControllerAnimated:YES];
4 }

3、保存照片成功后的回调

  • (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error *contextInfo:(void )contextInfo;
1 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
2   contextInfo:(void *)contextInfo{
3     
4 }

你可能感兴趣的:(ios —— UIImagePickerController类)