iOS 取得单张系统图片

这里主要用到了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:图片来源,有三个,照片流,图库,相机,区别和用法请参考代码

两个重要的代理方法

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

时间: 2024-12-16 21:33:50

iOS 取得单张系统图片的相关文章

ios替换app启动图片时系统报错的解决办法

ios替换app启动图片时系统报错的解决办法:我个人建议是在开发时候经常行的保存项目,并且在修改项目图标图片.app启动图片前,一定要先备份一份没有添加这两项图片的项目. 如果您的项目已经开发完成了,进入到发布前添加项目图标.app启动图片的时候,一定要确定了这两项的所有图片不会更改了再去添加,否则更改已经添加好的加项目图标.app启动图片就会报错, 如果您报错了,百度之后也没有解决办法,那么就尝试在已经备份的项目中重新去添加图片就可以了.

IOS常用的系统文件目录介绍

iOS常用目录整理说明是本文要介绍的内容,虽然不同API全面,也算是在编程中常用到的存放目录,所以是必备文档,不多说,来看详细内容讲解. 1.[/Applications] 常用软件的安装目录 内建软体及JB软体存放位置 2. [/private /var/ mobile/Media /iphone video Recorder] 录像文件存放目录 3.[/private /var/ mobile/Media /DCIM] 相机拍摄的照片文件存放目录 4.[/private/var/ mobil

IOS开发调用系统相机和打开闪光灯

IOS开发调用系统相机和打开闪光灯      今天给大家分享一下如何调用iphone的拍照功能和打开闪光灯,有些代码我也不太理解,很多是在网上借鉴其他人的.IOS有两种的拍照和视频的方 式:1.直接使用UIImagePickerController,这个类提供了一个简单便捷的拍照与选择图片库里图片的功能.2.另一种是通过 AVFoundation.framework框架完全自定义拍照的界面和选择图片库界面.我只做了第一种,就先给大家介绍第一种做法: 一.首先调用接口前,我们需要先判断当前设备是否

iOS开发利用系统推送Notifaction和轮询实现简单聊天系统

话不多说,先看一下做好的聊天软件界面: 首先在StoryBoard里拖了一个UItableView和一个view用来输入文字或者语音,右边的按钮用来切换文字和语音: 聊天里有三种id: orderID :聊天id messageID :每条消息的ID sessionID :每个订单的会话ID,如果为空通过orderID请求. 然后在viewDidLoad里做一些界面上的操作和一些初始化的操作: 1.设置一下tableview的headView 2.初始化录音.用户头像.获取订单详情 <stron

iOS.TextKit.02.文字图片混合排版

1.案例如图 2.代码 TextKit02ViewController.h #import <UIKit/UIKit.h> @interface TextKit02ViewController : UIViewController @property (nonatomic,strong) IBOutlet UITextView *textView; @property (nonatomic,weak) IBOutlet UIImageView *imageView; // 文本可以排版的区域

iOS一行代码压缩图片大小

现在基本所有应用都与图片相关联,这就必然涉及到上传下载图片,而用户的流量又迟迟没有被解放,因此图片就不能太大,我们知道iPhone一张照片动辄几M,如果都传原图那流量就会爆炸,粗暴地缩小又会影响图片的分辨率.那有没有办法在保持一定分辨率的情况下压缩图片呢?有的,而且非常简单,一行代码搞定,是苹果自带的压缩函数: UIImageJPEGRepresentation UIImagePNGRepresentation 这两个函数都是iOS自带的图片压缩工具.一个是压成JPEG格式,一个是压成PNG格式

iOS和android系统架构对比

iOS和android系统架构对比 iOS是基于UNIX的,直接与底层硬件通信.系统底层.应用框架.应用软件都是采用C/C++或者Objective-C写成的,所以有很高的运行效率. android是基于Linux内核设计,在Linux内核上运行一个Java虚拟机,虚拟机再运行软件.就好似在一个系统上又套了一个系统,以此内存消耗大,运行效率的.

IOS 按比例裁剪图片

拍照或者从图片库中获取图片 操作过程中容易闪退,也总会有内存压力警告,第一步,首先可以考虑裁剪图片,实际上可能不需要那么大的.其次可以考虑把耗时的比如存储过程放进线程. 这里封装裁剪图片的类方法. //NavView.m #define IMAGE_MAX_SIZE_WIDTH 640 #define IMAGE_MAX_SIZE_GEIGHT 1136 +(UIImage *)fitSmallImage:(UIImage *)image { if (nil == image) { return

iOS开发 点菜系统 使用UIPickerView

利用UIPickerView实现一个简单的点菜界面,如下图所示.源码地址:https://github.com/xiaoLong1010/iOSAppDemo.git UIPickerView有两个协议UIPickerViewDataSource,UIPickerViewDelegate,它们为UIPickeView提供数据和代理.UIPickeView的三个component分别代表fruit,main,drink,选中某一行,菜名在下面显示.最上面的随机按钮,则实现随机点菜功能. 1.vie