前言
UIKit框架提供了一个图像选择器UIImagePickerController
,我们不仅可以从相册中选择图像资源,还可以从照相机中直接获取拍摄的图像。
UIImagePickerController的创建
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
UIImagePickerController
继承于UINavigationController
,所以我们可以在当前通过以下方法调出:
[self presentViewController:imagePicker animated:YES completion:nil];
sourceType属性
sourceType
属性是设置UIImagePickerController
的图片选择源的,我们通过 sourceType
来设置将要从相册、相机还是相机胶卷选择图片。
@property(nonatomic) UIImagePickerControllerSourceType sourceType; // default value is UIImagePickerControllerSourceTypePhotoLibrary.
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
// 相册
UIImagePickerControllerSourceTypePhotoLibrary,
// 相机
UIImagePickerControllerSourceTypeCamera,
// 相机胶卷
UIImagePickerControllerSourceTypeSavedPhotosAlbum
} __TVOS_PROHIBITED;
相机胶卷是相册下的一个分类,该文件夹下的图片均是相机拍摄产生的图片
其他属性
- // 是否可以编辑
@property(nonatomic) BOOL allowsEditing NS_AVAILABLE_IOS(3_1); // replacement for -allowsImageEditing; default value is NO.
- // 是否可以编辑图像
@property(nonatomic) BOOL allowsImageEditing NS_DEPRECATED_IOS(2_0, 3_1);
- // 图像类型
@property(nonatomic,copy) NSArray<NSString *> *mediaTypes;
// default value is an array containing kUTTypeImage.
- // 视频类型图像最大时长
// video properties apply only if mediaTypes includes kUTTypeMovie
@property(nonatomic) NSTimeInterval videoMaximumDuration NS_AVAILABLE_IOS(3_1);
// default value is 10 minutes.
- // 图像质量类型
@property(nonatomic) UIImagePickerControllerQualityType videoQuality NS_AVAILABLE_IOS(3_1);
// default value is UIImagePickerControllerQualityTypeMedium. If the cameraDevice does not support the videoQuality, it will use the default value.
typedef NS_ENUM(NSInteger, UIImagePickerControllerQualityType) {
UIImagePickerControllerQualityTypeHigh = 0, // highest quality
UIImagePickerControllerQualityTypeMedium = 1, // medium quality, suitable for transmission via Wi-Fi
UIImagePickerControllerQualityTypeLow = 2, // lowest quality, suitable for tranmission via cellular network
UIImagePickerControllerQualityType640x480 NS_ENUM_AVAILABLE_IOS(4_0) = 3, // VGA quality
UIImagePickerControllerQualityTypeIFrame1280x720 NS_ENUM_AVAILABLE_IOS(5_0) = 4,
UIImagePickerControllerQualityTypeIFrame960x540 NS_ENUM_AVAILABLE_IOS(5_0) = 5,
} __TVOS_PROHIBITED;
- // 是否显示相机操作控件
// camera additions available only if sourceType is UIImagePickerControllerSourceTypeCamera.
@property(nonatomic) BOOL showsCameraControls NS_AVAILABLE_IOS(3_1);
// set to NO to hide all standard camera UI. default is YES
- // 预览视图
@property(nullable, nonatomic,strong) __kindof UIView *cameraOverlayView NS_AVAILABLE_IOS(3_1);
// set a view to overlay the preview view.
- // 预览视图的转换
@property(nonatomic) CGAffineTransform cameraViewTransform NS_AVAILABLE_IOS(3_1);
// set the transform of the preview view.
关于相机设备的属性
// 相机捕获类型
@property(nonatomic) UIImagePickerControllerCameraCaptureMode cameraCaptureMode NS_AVAILABLE_IOS(4_0); // default is UIImagePickerControllerCameraCaptureModePhoto
// 相机设备
@property(nonatomic) UIImagePickerControllerCameraDevice cameraDevice NS_AVAILABLE_IOS(4_0); // default is UIImagePickerControllerCameraDeviceRear
// 相机闪光类型
@property(nonatomic) UIImagePickerControllerCameraFlashMode cameraFlashMode NS_AVAILABLE_IOS(4_0); // default is UIImagePickerControllerCameraFlashModeAuto.
// cameraFlashMode controls the still-image flash when cameraCaptureMode is Photo. cameraFlashMode controls the video torch when cameraCaptureMode is Video.
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraCaptureMode) {
UIImagePickerControllerCameraCaptureModePhoto,
UIImagePickerControllerCameraCaptureModeVideo
} __TVOS_PROHIBITED;
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
UIImagePickerControllerCameraDeviceRear,
UIImagePickerControllerCameraDeviceFront
} __TVOS_PROHIBITED;
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {
UIImagePickerControllerCameraFlashModeOff = -1,
UIImagePickerControllerCameraFlashModeAuto = 0,
UIImagePickerControllerCameraFlashModeOn = 1
} __TVOS_PROHIBITED;
方法
- 类方法
/*是否是可用的读取源*/
+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;
// returns YES if source is available (i.e. camera present)
/*可用的读取源都有哪些*/
+ (nullable NSArray<NSString *> *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType;
// returns array of available media types (i.e. kUTTypeImage)
/*相机是否可用*/
+ (BOOL)isCameraDeviceAvailable:(UIImagePickerControllerCameraDevice)cameraDevice NS_AVAILABLE_IOS(4_0);
// returns YES if camera device is available
/*相机闪光是否可用*/
+ (BOOL)isFlashAvailableForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice NS_AVAILABLE_IOS(4_0);
// returns YES if camera device supports flash and torch.
/*可用的捕获类型都有哪些*/
+ (nullable NSArray<NSNumber *> *)availableCaptureModesForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice NS_AVAILABLE_IOS(4_0);
// returns array of NSNumbers (UIImagePickerControllerCameraCaptureMode)
- 实例方法
/*开始视频捕获*/
-(BOOL)startVideoCapture NS_AVAILABLE_IOS(4_0);
/*停止视频捕获*/
-(void)stopVideoCapture NS_AVAILABLE_IOS(4_0);
代理函数
@property(nullable,nonatomic,weak) id<UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;
@optional
// The picker does not dismiss itself; the client dismisses it in these callbacks.
// The delegate will receive one or the other, but not both, depending whether the user
// confirms or cancels.
/*选择图片完成回调*/
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0);
/*选择图像完成回调*/
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info;
/*点击‘取消’按钮回调*/
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
回调函数中的infoDictionary
的key
(我们可以通过这些key在回调函数的info字典中获得我们想要的内容):
// info dictionary keys
/*图像类型【NSString】*/
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaType __TVOS_PROHIBITED; // an NSString (UTI, i.e. kUTTypeImage)
/*原始图片【UIImage】*/
UIKIT_EXTERN NSString *const UIImagePickerControllerOriginalImage __TVOS_PROHIBITED; // a UIImage
/*编辑后的图片【UIImage】*/
UIKIT_EXTERN NSString *const UIImagePickerControllerEditedImage __TVOS_PROHIBITED; // a UIImage
/*剪裁大小【NSValue(CGRect)】*/
UIKIT_EXTERN NSString *const UIImagePickerControllerCropRect __TVOS_PROHIBITED; // an NSValue (CGRect)
/*图像url【NSURL】*/
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaURL __TVOS_PROHIBITED; // an NSURL
/*相关url【NSURL】*/
UIKIT_EXTERN NSString *const UIImagePickerControllerReferenceURL NS_AVAILABLE_IOS(4_1) __TVOS_PROHIBITED; // an NSURL that references an asset in the AssetsLibrary framework
/*媒体元数据【NSDictionary】*/
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaMetadata NS_AVAILABLE_IOS(4_1) __TVOS_PROHIBITED; // an NSDictionary containing metadata from a captured photo
/*PHLivePhoto类型的数据,【PHLivePhoto】是iOS9以后添加的一个新的图片类型,它不仅仅是一张静态的图片,它会抓取拍摄时的声音和更多帧的图像*/
UIKIT_EXTERN NSString *const UIImagePickerControllerLivePhoto NS_AVAILABLE_IOS(9_1) __TVOS_PROHIBITED; // a PHLivePhoto
应用
LMJImageChooseControl是我自己自定义的一个图片选择控件,这个控件实现了相册、相机选择获取图片的功能。
托管地址:https://github.com/MajorLMJ/LMJImageChooseControl
欢迎下载体验,同时理解UIImagePickerController
的使用。
有关相册、相机的访问权限
关于相册、相机的权限请阅读:iOS访问相册、相机权限
版权声明:出自MajorLMJ技术博客的原创作品 ,转载时必须注明出处及相应链接!