图像选择控制器:UIImagePickerController
功能:用于选取相册或相机等里面的照片。
@interface UIImagePickerController : UINavigationController
枚举:
//图片资源来源类型
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary, //图库
UIImagePickerControllerSourceTypeCamera, //相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum //相册
};
//摄像图像的质量
typedef NS_ENUM(NSInteger, UIImagePickerControllerQualityType) {
UIImagePickerControllerQualityTypeHigh = 0, // 高质量
UIImagePickerControllerQualityTypeMedium = 1, //中等质量,适合通过Wi-Fi传输
UIImagePickerControllerQualityTypeLow = 2, // 最低的质量,适合通过蜂窝网络传输
UIImagePickerControllerQualityType640x480 = 3, // VGA质量,640*480分辨率
UIImagePickerControllerQualityTypeIFrame1280x720 = 4, // VGA质量1280*720分辨率
UIImagePickerControllerQualityTypeIFrame960x540 = 5 // VGA质量,960*540分辨率
};
//相机摄像模式
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraCaptureMode) {
UIImagePickerControllerCameraCaptureModePhoto, //图片
UIImagePickerControllerCameraCaptureModeVideo //录像
};
//摄像头位置
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraDevice) {
UIImagePickerControllerCameraDeviceRear, //后摄像头
UIImagePickerControllerCameraDeviceFront //前摄像头
};
//闪光灯状态模式
typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {
UIImagePickerControllerCameraFlashModeOff = -1, //关闭状态
UIImagePickerControllerCameraFlashModeAuto = 0, //自动调节状态
UIImagePickerControllerCameraFlashModeOn = 1 //开启状态
};
图片信息中字典的键:
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaType; //用户自定义的媒体类型
UIKIT_EXTERN NSString *const UIImagePickerControllerOriginalImage; // 原始的图片
UIKIT_EXTERN NSString *const UIImagePickerControllerEditedImage; // 编辑后的图片
UIKIT_EXTERN NSString *const UIImagePickerControllerCropRect; // 裁剪尺寸
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaURL; // 媒体URL
UIKIT_EXTERN NSString *const UIImagePickerControllerReferenceURL; // 原件URL
UIKIT_EXTERN NSString *const UIImagePickerControllerMediaMetadata // 仅有效于拍摄图片时
属性:
//图像选取代理
@property(nonatomic,assign) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;
//图片资源来源类型,默认来自图库
@property(nonatomic)UIImagePickerControllerSourceType sourceType;
//媒体类型数组
@property(nonatomic,copy)NSArray *mediaTypes;
//是否允许编辑图像,默认为NO
@property(nonatomic) BOOL allowsEditing ;
@property(nonatomic) BOOL allowsImageEditing ;
//录像最大时长,默认为10分钟
@property(nonatomic) NSTimeInterval videoMaximumDuration;
//录像质量
@property(nonatomic) UIImagePickerControllerQualityType videoQuality ;
//是否显示相机控制界面,默认显示
@property(nonatomic) BOOL showsCameraControls ;
//设置视图覆盖预览视图
@property(nonatomic,retain) UIView *cameraOverlayView ;
//相机视图仿射类型
@property(nonatomic) CGAffineTransform cameraViewTransform ;
//相机摄像模式,默认为拍摄图片
@property(nonatomic) UIImagePickerControllerCameraCaptureMode cameraCaptureMode ;
//摄像头选取,默认为后摄像头
@property(nonatomic) UIImagePickerControllerCameraDevice cameraDevice ;
//闪光灯状态模式
@property(nonatomic) UIImagePickerControllerCameraFlashMode cameraFlashMode;
方法:
//资源来源类型是否可用
+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;
//返回资源来源类型中可用的媒体类型数组
+ (NSArray *)availableMediaTypesForSourceType:(UIImagePickerControllerSourceType)sourceType;
//摄像头是否可用
+ (BOOL)isCameraDeviceAvailable:(UIImagePickerControllerCameraDevice)cameraDevice ;
//闪光灯是否可用
+ (BOOL)isFlashAvailableForCameraDevice:(UIImagePickerControllerCameraDevice)cameraDevice ;
//返回可用的相机模式数组
+ (NSArray *)availableCaptureModesForCameraDevice (UIImagePickerControllerCameraDevice)
cameraDevice;
//照相
- (void)takePicture;
//开始录频
- (BOOL)startVideoCapture ;
//停止录频
- (void)stopVideoCapture ;
代理:
@protocol UIImagePickerControllerDelegate<NSObject>
@optional
//选取编辑过的图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo ;
//选取原始图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info;
//取消选取
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
@end
保存相关的函数:
// 保存图片到被保存的相册中
UIKIT_EXTERN void UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);
// - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
// 保存指定路径的录频到被保存的相册中
UIKIT_EXTERN BOOL UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(NSString *videoPath) ;
// 保存录频到被保存的相册中
UIKIT_EXTERN void UISaveVideoAtPathToSavedPhotosAlbum(NSString *videoPath, id completionTarget, SEL completionSelector, void *contextInfo);
//- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
下面来举一个实例如下:从本地相册读取显示图片
1、设置故事板,往故事板的视图中拖入两个控件,一个是UIImageView,另一个是UIButton,分别命名为imageView、“相册”。
2、将imageView关联IBOutLet到控制器类中作为属性,给"相册"按钮关联IBAction事件,用来显示功能。还要让类实现图像选取器控制器协议和导航控制器协议
3、现在就是显示功能的实现和协议方法的实现了。
//按钮事件,打开相册(以模态窗口的形式显示相册)
- (IBAction)buttonClicked:(UIButton *)sender { //创建图片选取控制器 UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
//图片资源来自相册 imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//设置选取代理 imagePicker.delegate = self; //使用模态窗口显示相册 [self presentViewController:imagePicker animated:YES completion:nil]; } - (void)viewDidLoad { [super viewDidLoad]; }
//实现协议方法,选取相册中的相片:
#pragma mark -<UIImagePickercontrollerDelegate>
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //NSLog(@"%@",info);
/*info:这个字典中放着图片的具体信息,通过这个字典的键可以获取图片*/
//取出图片 self.image = [info objectForKey:UIImagePickerControllerOriginalImage];//显示图片在图像视图中 [self.imageView setImage:self.image]; //关闭模态窗口 [picker dismissViewControllerAnimated:YES completion:nil]; }
4、演示结果:
开始运行截图: 显示的本地相册截图:
选取某一图片截图: 点击"相册"返回,再选取某一图片截图: