标签:
如果我们仅仅是为了访问相册,那我们做的工作十分简单。
1、UIImagePickerController初始化
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
2、设置属性
1)sourceType属性:数据的来源,有三种来源:
enum {
UIImagePickerControllerSourceTypePhotoLibrary, // 图库
UIImagePickerControllerSourceTypeCamera, // 相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相册
};
typedef NSUInteger UIImagePickerControllerSourceType;
2)allowsEditing属性:是否允许剪切
3)设置代理deleage
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];
查看相册图片,并更换图片简单实现:
1 #import "ViewController.h" 2 #import "SJZViewController.h" 3 4 @interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate> 5 6 @property (nonatomic, strong) UIImage * image; 7 @property (nonatomic, strong) UIImageView * imageView; 8 @property (nonatomic, strong) UIImagePickerController * pickerController; 9 10 @end 11 12 @implementation ViewController 13 14 - (void)viewDidLoad { 15 [super viewDidLoad]; 16 self.title = @"选取图片"; 17 18 UIImage * image = [UIImage imageNamed:@"saveImage.jpg"]; 19 self.image = image; 20 21 UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2)]; 22 imageView.center = self.view.center; 23 imageView.image = image; 24 self.imageView = imageView; 25 [self.view addSubview:imageView]; 26 27 [self createButton]; 28 } 29 30 - (void)createButton 31 { 32 UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(40, self.view.frame.size.height * 7 / 8, self.view.frame.size.width - 80, 40)]; 33 button.backgroundColor = [UIColor whiteColor]; 34 [button setTitle:@"从相册选取图片" forState:UIControlStateNormal]; 35 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 36 button.layer.cornerRadius = 4.0; 37 [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside]; 38 [self.view addSubview:button]; 39 } 40 41 //创建UIImagePickerController对象,懒加载 42 - (UIImagePickerController *)pickerController 43 { 44 if(!_pickerController){ 45 //创建UIImagePickerController对象 46 UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 47 48 UIImagePickerController * picker = [[UIImagePickerController alloc]init]; 49 picker.delegate = self; 50 picker.allowsEditing = YES; 51 picker.sourceType = sourceType; 52 53 _pickerController = picker; 54 } 55 56 return _pickerController; 57 } 58 59 - (void)buttonClick 60 { 61 if([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0){ 62 UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; 63 64 //相机 65 UIAlertAction * action = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 66 //未实现,下期实现 67 68 }]; 69 [action setValue:[UIColor blackColor] forKey:@"_titleTextColor"]; 70 [alert addAction:action]; 71 72 //访问相册 73 UIAlertAction * photoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 74 75 [self presentViewController:self.pickerController animated:YES completion:nil]; 76 }]; 77 [photoAction setValue:[UIColor blackColor] forKey:@"_titleTextColor"]; 78 [alert addAction:photoAction]; 79 80 UIAlertAction * cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 81 82 }]; 83 [cancleAction setValue:[UIColor redColor] forKey:@"_titleTextColor"]; 84 [alert addAction:cancleAction]; 85 86 [self presentViewController:alert animated:YES completion:nil]; 87 }else{ 88 UIActionSheet * sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机", @"从相册选择", nil]; 89 90 [sheet showInView:self.view]; 91 } 92 } 93 94 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 95 { 96 if(buttonIndex == 0){ 97 98 }else if(buttonIndex == 1){ 99 [self presentViewController:self.pickerController animated:YES completion:nil]; 100 } 101 } 102 103 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info 104 { 105 self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage]; 106 107 [picker dismissViewControllerAnimated:YES completion:nil]; 108 } 109 110 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 111 { 112 [picker dismissViewControllerAnimated:YES completion:nil]; 113 } 114 115 @end
显示结果:
有关相机的部分,下篇会实现。。。。。。。
ios学习笔记——UIImagePickerController访问相册
标签:
原文地址:http://www.cnblogs.com/sjzlovecj/p/5561802.html