iOS 图像选取器UIImagePickerController

UIImagePickerController

UIImagePickerController可以帮助我们调用摄像头拍照或者希望从相册中选择照片。

UIImagePickerController主要属性

// 图片获取来源
@property(nonatomic) UIImagePickerControllerSourceType sourceType;

// 拍完照或相册选择图片后,是否允许编辑图像
@property(nonatomic) BOOL allowsEditing;

// 媒体类型,public.image和public.movie,默认是public.image
@property(nonatomic,copy) NSArray<NSString *> *mediaTypes;

// 代理设置,获取图片
@property(nullable,nonatomic,weak) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;

UIImagePickerControllerSourceType表示图片获取来源

typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
    UIImagePickerControllerSourceTypePhotoLibrary, // 照片库
    UIImagePickerControllerSourceTypeCamera, // 相机
    UIImagePickerControllerSourceTypeSavedPhotosAlbum, // 相册
}

相机相关属性

// 是否显示相机控制按钮,默认是YES
@property(nonatomic) BOOL showsCameraControls;

// 自定义相机控制页面
@property(nullable, nonatomic,strong) UIView *cameraOverlayView;

// 拍摄照片还是视频,默认是UIImagePickerControllerCameraCaptureModePhoto
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraCaptureMode) {
    UIImagePickerControllerCameraCaptureModePhoto,
    UIImagePickerControllerCameraCaptureModeVideo
};
@property(nonatomic) UIImagePickerControllerCameraCaptureMode cameraCaptureMode;
   
// 摄像头,默认是UIImagePickerControllerCameraDeviceRear
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
    UIImagePickerControllerCameraDeviceRear,
    UIImagePickerControllerCameraDeviceFront
};
@property(nonatomic) UIImagePickerControllerCameraDevice cameraDevice;

// 闪光灯模式,默认是UIImagePickerControllerCameraFlashModeAuto
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {
    UIImagePickerControllerCameraFlashModeOff  = -1,
    UIImagePickerControllerCameraFlashModeAuto = 0,
    UIImagePickerControllerCameraFlashModeOn   = 1
};
@property(nonatomic) UIImagePickerControllerCameraFlashMode cameraFlashMode; 

// 录像最大时间,默认为十分钟
@property(nonatomic) NSTimeInterval videoMaximumDuration;

// 录像像素
typedef NS_ENUM(NSInteger, UIImagePickerControllerQualityType) {
    UIImagePickerControllerQualityTypeHigh = 0,       // highest quality
    UIImagePickerControllerQualityTypeMedium = 1,     // medium quality,默认
    UIImagePickerControllerQualityTypeLow = 2,        // lowest quality
    UIImagePickerControllerQualityType640x480 = 3,    // VGA quality
    UIImagePickerControllerQualityTypeIFrame1280x720 = 4,
    UIImagePickerControllerQualityTypeIFrame960x540 = 5,
}
@property(nonatomic) UIImagePickerControllerQualityType videoQuality;

UIImagePickerController主要方法

// 判断设备是否支持照片库/相机/相册
+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType; 

// 判断设备照片库/相机/相册支持的媒体类型
+ (nullable NSArray<NSString *> *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType;

// 判断设备是否支持前置摄像头/后置摄像头
+ (BOOL)isCameraDeviceAvailable:(UIImagePickerControllerCameraDevice)cameraDevice;

// 判断设备前置摄像头/后置摄像头是否支持闪光灯
+ (BOOL)isFlashAvailableForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice;

// 判断设备前置摄像头/后置摄像头是否支持拍照/视频
+ (nullable NSArray<NSNumber *> *)availableCaptureModesForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice;

// 拍照
- (void)takePicture

// 开始录像 
- (BOOL)startVideoCapture;

// 停止录像
- (void)stopVideoCapture;

UIImagePickerController功能

从照片库获取图片

UIImagePickerController *vc = [[UIImagePickerController alloc] init];
vc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
vc.allowsEditing = YES;
vc.delegate = self;

[self presentViewController:vc animated:YES completion:nil];

自拍

UIImagePickerController *vc = [[UIImagePickerController alloc] init];
vc.sourceType = UIImagePickerControllerSourceTypeCamera;
vc.cameraDevice = UIImagePickerControllerCameraDeviceFront;
vc.delegate = self;

[self presentViewController:vc animated:YES completion:nil];

录像

UIImagePickerController *vc = [[UIImagePickerController alloc] init];
vc.sourceType = UIImagePickerControllerSourceTypeCamera;
vc.mediaTypes = @[@"public.movie"];
vc.delegate = self;

[self presentViewController:vc animated:YES completion:nil];

UIImagePickerControllerDelegate

UIImagePickerControllerDelegate代理协议提供了用户选择图片后的回调方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
    UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    NSURL *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
    
    if ([@"public.image" isEqualToString:mediaType]) {
        if (editedImage) {
            self.pickerImageView.image = editedImage;
        } else {
            self.pickerImageView.image = originalImage;
        }
    } else if ([@"public.movie" isEqualToString:mediaType]) {
    }
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {    
    [picker dismissViewControllerAnimated:YES completion:nil];
}

info包含了采集的图片/视频信息

// 媒体类型
UIImagePickerControllerInfoKey const UIImagePickerControllerMediaType;
// 原始的UIImage
UIImagePickerControllerInfoKey const UIImagePickerControllerOriginalImage;
// 编辑后的UIImage
UIImagePickerControllerInfoKey const UIImagePickerControllerEditedImage;
// 编辑裁剪区域
UIImagePickerControllerInfoKey const UIImagePickerControllerCropRect;
// 视频文件链接
UIImagePickerControllerInfoKey const UIImagePickerControllerMediaURL;

权限

需要在Info.plist文件中,添加相机、相册和麦克风权限
iOS 图像选取器UIImagePickerController_第1张图片

你可能感兴趣的:(iOS,图像动画,ios,UIImagePicker)