1、UIImagePickerController初始化
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
2、设置属性
1)sourceType属性:数据的来源,有三种来源:
enum {
UIImagePickerControllerSourceTypePhotoLibrary, // 图库
UIImagePickerControllerSourceTypeCamera, // 相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相册
};
typedef NSUInteger UIImagePickerControllerSourceType;
2)allowsEditing属性:是否允许剪切
3)设置代理deleage
4) mediaTypes属性:为UIImagePickerViewController指定媒体类型(数组),默认包含kUTTypeImage,所以拍照时可以不用设置,但是录像的时候必须设置,可以设置为kUTTypeVideo(不带声音)或者kUTTypeMovie(视频并带有声音)
设置媒体类型:
1 picker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeMovie, nil];
如果想包含所有媒体,可以使用下面方法:
1 picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
5) videoQuality属性:视频质量,一个枚举类型。
1 enum { 2 UIImagePickerControllerQualityTypeHigh = 0, //高清 3 UIImagePickerControllerQualityTypeMedium = 1, // default value 4 UIImagePickerControllerQualityTypeLow = 2, //低质量,适用于蜂窝网传输 5 UIImagePickerControllerQualityType640x480 = 3, 6 UIImagePickerControllerQualityTypeIFrame1280x720 = 4, 7 UIImagePickerControllerQualityTypeIFrame960x540 = 5 8 }; 9 typedef NSUInteger UIImagePickerControllerQualityType;
6) videoMaximumDuration属性:录制视频的最长时间,默认长度为10分钟。
7) showsCameraControls属性:是否显示摄像头控制面板,默认为YES。
3、显示UIImagePickerController
[self presentViewController:self.pickerController animated:YES completion:nil];
4、实现代理函数
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
如果实现了imagePickerControllerDidCancel函数,那么我们点击cancel按钮时,失效,所以我们要在这个函数里调用
[picker dismissViewControllerAnimated:YES completion:nil];
简单实现拍照、录像,并从相册中取照片和视频:
SJZPhotoViewController.h
1 #import <UIKit/UIKit.h> 2 3 @protocol SJZPhotoViewControllerDelegate <NSObject> 4 5 @optional 6 - (void)getPhotoFromXiangce:(UIImage *)image; 7 - (void)getVideoFromXiangce:(NSURL *)strURL; 8 @end 9 10 @interface SJZPhotoViewController : UIViewController 11 12 @property (nonatomic, weak) UIViewController * parentController; 13 @property (nonatomic, weak) id<SJZPhotoViewControllerDelegate> delegate; 14 @property (nonatomic, assign) CGFloat imageSale; 15 16 17 - (void)addPhotoActionSheet; 18 - (void)addVideoActionSheet; 19 @end
SJZPhotoViewController.m
1 #import "SJZPhotoViewController.h" 2 #import <MobileCoreServices/UTCoreTypes.h> 3 4 @interface SJZPhotoViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate> 5 6 @end 7 8 @implementation SJZPhotoViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 14 } 15 16 - (void)addVideoActionSheet 17 { 18 UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 19 20 UIAlertAction * videoAction = [UIAlertAction actionWithTitle:@"录制视频" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 21 [self actionsheetForVideofromXiangji]; 22 }]; 23 [alert addAction:videoAction]; 24 25 UIAlertAction * action = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 26 [self aciontSheetForVideoFromXiangce]; 27 }]; 28 29 [alert addAction:action]; 30 31 UIAlertAction * cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 32 33 }]; 34 [alert addAction:cancleAction]; 35 36 [self.parentController presentViewController:alert animated:YES completion:^{ 37 38 }]; 39 40 } 41 42 //从相册取视频 43 - (void)aciontSheetForVideoFromXiangce 44 { 45 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){ 46 UIImagePickerController * picker = [[UIImagePickerController alloc] init]; 47 picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 48 //设置媒体类型 49 picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; 50 picker.delegate = self; 51 52 [self.parentController presentViewController:picker animated:YES completion:^{ 53 54 }]; 55 } 56 } 57 58 //录像 59 - (void)actionsheetForVideofromXiangji 60 { 61 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ 62 //判断后置摄像头是否可用 63 if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]){ 64 UIImagePickerController * picker = [[UIImagePickerController alloc] init]; 65 picker.sourceType = UIImagePickerControllerSourceTypeCamera; 66 picker.delegate = self; 67 68 //设置媒体类型,需要包括视频媒体类型 69 picker.mediaTypes = [NSArray arrayWithObjects:(NSString *)kUTTypeMovie, nil]; 70 71 [self.parentController presentViewController:picker animated:YES completion:^{ 72 73 }]; 74 } 75 } 76 } 77 78 - (void)addPhotoActionSheet 79 { 80 UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 81 82 UIAlertAction * action = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 83 [self actionSheetForImageFromXiangji]; 84 }]; 85 [alert addAction:action]; 86 87 UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 88 [self aciontSheetForImageFromXiangce]; 89 }]; 90 [alert addAction:action1]; 91 92 UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 93 94 }]; 95 [alert addAction:cancelAction]; 96 97 [self.parentController presentViewController:alert animated:YES completion:nil]; 98 } 99 100 //从相册区图片 101 - (void)aciontSheetForImageFromXiangce 102 { 103 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){ 104 UIImagePickerController * picker = [[UIImagePickerController alloc] init]; 105 picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 106 picker.delegate = self; 107 [self.parentController presentViewController:picker animated:YES completion:^{ 108 }]; 109 } 110 } 111 112 //相机拍照 113 - (void)actionSheetForImageFromXiangji 114 { 115 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ 116 UIImagePickerController * pick = [[UIImagePickerController alloc] init]; 117 pick.sourceType = UIImagePickerControllerSourceTypeCamera; 118 pick.delegate = self; 119 120 [self.parentController presentViewController:pick animated:YES completion:^{ 121 }]; 122 } 123 } 124 125 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info 126 { 127 NSString * meidaType = [info objectForKey:UIImagePickerControllerMediaType]; 128 if([meidaType isEqualToString:@"public.image"]){ 129 UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage]; 130 //在这里可以对图片进行处理(比如压缩) 131 if(self.imageSale > 0){ 132 NSData * photoData = UIImageJPEGRepresentation(image, self.imageSale); 133 image = [UIImage imageWithData:photoData]; 134 } 135 136 if([self.delegate respondsToSelector:@selector(getPhotoFromXiangce:)]){ 137 [self.delegate getPhotoFromXiangce:image]; 138 } 139 }else if([meidaType isEqualToString:(NSString *)kUTTypeMovie]){ 140 //获取视频文件的url 141 NSURL * mediaURL = [info objectForKey:UIImagePickerControllerMediaURL]; 142 143 if([self.delegate respondsToSelector:@selector(getVideoFromXiangce:)]){ 144 [self.delegate getVideoFromXiangce:mediaURL]; 145 } 146 } 147 148 [self.parentController dismissViewControllerAnimated:YES completion:nil]; 149 } 150 151 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 152 { 153 [self.parentController dismissViewControllerAnimated:YES completion:^{ 154 155 }]; 156 } 157 158 @end