码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 取得单张系统图片

时间:2016-01-26 20:20:23      阅读:756      评论:0      收藏:0      [点我收藏+]

标签:

这里主要用到了UIImagePickerController

不多废话,直接上代码

技术分享
  1 //
  2 //  RootViewController.m
  3 //  GetImageFromPhotoAlbum
  4 //
  5 //  Created by 王云龙 on 16/1/18.
  6 //  Copyright © 2016年 王云龙. All rights reserved.
  7 //
  8 
  9 #import "RootViewController.h"
 10 #import "SecViewController.h"
 11 
 12 @interface RootViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>
 13 
 14 {
 15     UIImageView *_imageView;
 16     UIImageView *_imageViewR;
 17 }
 18 
 19 @end
 20 
 21 @implementation RootViewController
 22 
 23 
 24 /**
 25  *  1.导航器
 26  *  2.UIImagePickerController
 27  *  3.iOS文件存取,沙盒机制
 28  */
 29 
 30 - (void)viewDidLoad
 31 {
 32     [super viewDidLoad];
 33     // Do any additional setup after loading the view.
 34     
 35 #pragma mark - 导航器设置
 36     [self.navigationController.navigationBar setTranslucent:NO];//设置navigationbar的半透明
 37     NSLog(@"frame = %@,bounds = %@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.window.bounds));
 38     self.title = @"navigationcontroller";//设置navigationbar上显示的标题
 39     [self.navigationController.navigationBar setBarTintColor:[UIColor purpleColor]];//设置navigationbar的颜色
 40     
 41     UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leftAction:)];
 42     self.navigationItem.leftBarButtonItem = left; //设置navigationbar左边按钮
 43     
 44     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(rightAction:)];//设置navigationbar右边按钮
 45     [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];//设置navigationbar上左右按钮字体颜色
 46     
 47 #pragma mark - 画面初始化
 48     _imageViewR = [[UIImageView alloc]initWithFrame:CGRectMake(16, 20, 100, 100)];
 49     _imageViewR.backgroundColor = [UIColor grayColor];
 50     [self.view addSubview:_imageViewR];
 51     
 52     _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(16, 140, self.view.frame.size.width-32, self.view.frame.size.width-32)];
 53     _imageView.backgroundColor = [UIColor grayColor];
 54     [self.view addSubview:_imageView];
 55     
 56     UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
 57     btn.frame = CGRectMake(16, CGRectGetMaxY(_imageView.frame)+20, self.view.frame.size.width-32, 35);
 58     btn.layer.cornerRadius = 5;
 59     btn.layer.masksToBounds = YES;
 60     [btn setTitle:@"获取相册图片" forState:UIControlStateNormal];
 61     [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
 62     [self.view addSubview:btn];
 63     
 64 }
 65 
 66 #pragma mark - 导航控制器的跳转
 67 -(void)leftAction:(UIBarButtonItem*)sender{
 68     //只能跳到没有没有导航控制器的controller
 69     SecViewController *secVC = [[SecViewController alloc]init];
 70     [self.navigationController pushViewController:secVC animated:YES];
 71 }
 72 
 73 -(void)rightAction:(UIBarButtonItem*)sender{
 74     //相册
 75     UIImagePickerController *imagePickController = [[UIImagePickerController alloc]init];
 76     imagePickController.delegate = self;
 77     imagePickController.allowsEditing = YES;
 78     imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
 79     [self presentViewController:imagePickController animated:YES completion:nil
 80      ];
 81     
 82     
 83 }
 84 
 85 #pragma mark - 相册中取得图片并显示
 86 -(void)btnAction:(UIButton*)sender{
 87     
 88     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"获取图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
 89     
 90     //判断支持类型
 91     //相册和图库的区别
 92     //http://zhidao.baidu.com/link?url=DGZvUPsBPpArTyqP5ff2BcbVL1s_OUuH9A4TfB3Bn0xTP_iylo7Y45wAShBGZXDW85cicNPaeXSQlLiPzYCvCbDmA9gPHX5042sNfrN5ZFu
 93     
 94     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
 95         NSLog(@"支持相机");
 96     else
 97         NSLog(@"不支持相机");
 98     
 99     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
100         NSLog(@"支持图库");
101     else
102         NSLog(@"不支持图库");
103     
104     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
105         NSLog(@"支持相册");
106     else
107         NSLog(@"不支持相册");
108     
109     
110     
111     //判断是否支持相机
112     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
113         UIAlertAction*defualtAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
114             UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
115             imagePickerController.delegate = self;
116             imagePickerController.allowsEditing = YES;
117             imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
118             [self presentViewController:imagePickerController animated:YES completion:^{}];
119         }];
120         [alertController addAction:defualtAction];
121     }
122     UIAlertAction *defaultAction1 = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
123         //相册
124         UIImagePickerController *imagePickController = [[UIImagePickerController alloc]init];
125         imagePickController.delegate = self;
126         imagePickController.allowsEditing = YES;
127         imagePickController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
128         [self presentViewController:imagePickController animated:YES completion:nil
129          ];
130         
131     }];
132     UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
133     [alertController addAction:cancelAction];
134     [alertController addAction:defaultAction1];
135     
136     //弹出视图
137     [self presentViewController:alertController animated:YES completion:nil];
138     
139 }
140 
141 #pragma mark - 文件保存到本地
142 //http://blog.csdn.net/jianjianyuer/article/details/8556024
143 -(void)saveImage:(UIImage*)currentImage withName:(NSString*)name{
144     //图片读取的两个方法
145     //http://blog.csdn.net/lovenjoe/article/details/7484217
146     //NSData存储二进制数据
147     //http://blog.csdn.net/jjmm2009/article/details/39004149
148     NSData *imageData = UIImagePNGRepresentation(currentImage);
149     
150     //沙盒介绍
151     //http://www.cnblogs.com/taintain1984/archive/2013/03/19/2969201.html
152     NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:name];
153     
154     //沙盒文件操作
155     //http://www.cocoachina.com/bbs/read.php?tid-78784.html
156     
157     //将图片写入
158     [imageData writeToFile:fullPath atomically:YES];
159 }
160 
161 #pragma mark - UIImagePickerController的代理方法。
162 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
163     
164     [picker dismissViewControllerAnimated:YES completion:nil];
165     //得到原始图片,未编辑的
166     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
167     
168     //判断是否图片已经读取过
169     NSString*referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
170     NSLog(@"%@",referenceURL);
171     //保存图片到本地
172     //文件名为时间戳,防止重名
173     NSDate *datenow = [NSDate date];
174     NSString *timeSp = [NSString stringWithFormat:@"%lf",[datenow timeIntervalSince1970]];
175     NSString *fileName = [NSString stringWithFormat:@"%@.png",timeSp];
176     
177     [self saveImage:image withName:fileName];
178     
179     NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:fileName];
180     
181     
182     UIImage *saveImage = [[UIImage alloc]initWithContentsOfFile:fullPath];
183 
184     //设置图片显示
185     [_imageView setImage:saveImage];
186     [_imageViewR setImage:image];
187 }
188 
189 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
190     [picker dismissViewControllerAnimated:YES completion:nil];
191 }
192 
193 
194 - (void)didReceiveMemoryWarning {
195     [super didReceiveMemoryWarning];
196     // Dispose of any resources that can be recreated.
197 }
198 
199 
200 @end
取得图片的代码

注意点

需要设置的三个重要属性

delegate:代理,必须设置,否则选择图片后页面跳转不会来,也取不到选取的图片

allowEditing:最好设置为yes,否则看不到原图,只能在图片墙上选取图片

souceType:图片来源,有三个,照片流,图库,相机,区别和用法请参考代码

两个重要的代理方法

请参考代码,但是两个代理方法中都要写模态跳转,系统是不会自己跳回来的。

 

iOS 取得单张系统图片

标签:

原文地址:http://www.cnblogs.com/ieso-ios/p/5161215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!