这里主要用到了UIImagePickerController

不多废话,直接上代码

 //
// RootViewController.m
// GetImageFromPhotoAlbum
//
// Created by 王云龙 on 16/1/18.
// Copyright © 2016年 王云龙. All rights reserved.
// #import "RootViewController.h"
#import "SecViewController.h" @interface RootViewController ()<UIActionSheetDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
UIImageView *_imageView;
UIImageView *_imageViewR;
} @end @implementation RootViewController /**
* 1.导航器
* 2.UIImagePickerController
* 3.iOS文件存取,沙盒机制
*/ - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. #pragma mark - 导航器设置
[self.navigationController.navigationBar setTranslucent:NO];//设置navigationbar的半透明
NSLog(@"frame = %@,bounds = %@",NSStringFromCGRect(self.view.frame),NSStringFromCGRect(self.view.window.bounds));
self.title = @"navigationcontroller";//设置navigationbar上显示的标题
[self.navigationController.navigationBar setBarTintColor:[UIColor purpleColor]];//设置navigationbar的颜色 UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leftAction:)];
self.navigationItem.leftBarButtonItem = left; //设置navigationbar左边按钮 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(rightAction:)];//设置navigationbar右边按钮
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];//设置navigationbar上左右按钮字体颜色 #pragma mark - 画面初始化
_imageViewR = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];
_imageViewR.backgroundColor = [UIColor grayColor];
[self.view addSubview:_imageViewR]; _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width-, self.view.frame.size.width-)];
_imageView.backgroundColor = [UIColor grayColor];
[self.view addSubview:_imageView]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(, CGRectGetMaxY(_imageView.frame)+, self.view.frame.size.width-, );
btn.layer.cornerRadius = ;
btn.layer.masksToBounds = YES;
[btn setTitle:@"获取相册图片" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn]; } #pragma mark - 导航控制器的跳转
-(void)leftAction:(UIBarButtonItem*)sender{
//只能跳到没有没有导航控制器的controller
SecViewController *secVC = [[SecViewController alloc]init];
[self.navigationController pushViewController:secVC animated:YES];
} -(void)rightAction:(UIBarButtonItem*)sender{
//相册
UIImagePickerController *imagePickController = [[UIImagePickerController alloc]init];
imagePickController.delegate = self;
imagePickController.allowsEditing = YES;
imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickController animated:YES completion:nil
]; } #pragma mark - 相册中取得图片并显示
-(void)btnAction:(UIButton*)sender{ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"获取图片" message:nil preferredStyle:UIAlertControllerStyleActionSheet]; //判断支持类型
//相册和图库的区别
//http://zhidao.baidu.com/link?url=DGZvUPsBPpArTyqP5ff2BcbVL1s_OUuH9A4TfB3Bn0xTP_iylo7Y45wAShBGZXDW85cicNPaeXSQlLiPzYCvCbDmA9gPHX5042sNfrN5ZFu if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
NSLog(@"支持相机");
else
NSLog(@"不支持相机"); if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
NSLog(@"支持图库");
else
NSLog(@"不支持图库"); if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
NSLog(@"支持相册");
else
NSLog(@"不支持相册"); //判断是否支持相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertAction*defualtAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}];
[alertController addAction:defualtAction];
}
UIAlertAction *defaultAction1 = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//相册
UIImagePickerController *imagePickController = [[UIImagePickerController alloc]init];
imagePickController.delegate = self;
imagePickController.allowsEditing = YES;
imagePickController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imagePickController animated:YES completion:nil
]; }];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:defaultAction1]; //弹出视图
[self presentViewController:alertController animated:YES completion:nil]; } #pragma mark - 文件保存到本地
//http://blog.csdn.net/jianjianyuer/article/details/8556024
-(void)saveImage:(UIImage*)currentImage withName:(NSString*)name{
//图片读取的两个方法
//http://blog.csdn.net/lovenjoe/article/details/7484217
//NSData存储二进制数据
//http://blog.csdn.net/jjmm2009/article/details/39004149
NSData *imageData = UIImagePNGRepresentation(currentImage); //沙盒介绍
//http://www.cnblogs.com/taintain1984/archive/2013/03/19/2969201.html
NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:name]; //沙盒文件操作
//http://www.cocoachina.com/bbs/read.php?tid-78784.html //将图片写入
[imageData writeToFile:fullPath atomically:YES];
} #pragma mark - UIImagePickerController的代理方法。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ [picker dismissViewControllerAnimated:YES completion:nil];
//得到原始图片,未编辑的
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; //判断是否图片已经读取过
NSString*referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
NSLog(@"%@",referenceURL);
//保存图片到本地
//文件名为时间戳,防止重名
NSDate *datenow = [NSDate date];
NSString *timeSp = [NSString stringWithFormat:@"%lf",[datenow timeIntervalSince1970]];
NSString *fileName = [NSString stringWithFormat:@"%@.png",timeSp]; [self saveImage:image withName:fileName]; NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:fileName]; UIImage *saveImage = [[UIImage alloc]initWithContentsOfFile:fullPath]; //设置图片显示
[_imageView setImage:saveImage];
[_imageViewR setImage:image];
} -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

取得图片的代码

注意点

需要设置的三个重要属性

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

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

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

两个重要的代理方法

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

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

  1. iOS UIButton文字和图片间距随意调整

    代码地址如下:http://www.demodashi.com/demo/11606.html 前记 在开发中,我们经常会遇到这么一种情况,就是一个按钮上面有图片也有文字,但是往往设计并不是我们想要的 ...

  2. iOS 解决LaunchScreen中图片加载黑屏问题

    iOS 解决LaunchScreen中图片加载黑屏问题 原文: http://blog.csdn.net/chengkaizone/article/details/50478045 iOS 解决Lau ...

  3. iOS根据Url 获取图片尺寸

    iOS根据Url 获取图片尺寸 // 根据图片url获取图片尺寸 +(CGSize)getImageSizeWithURL:(id)imageURL { NSURL* URL = nil; if([i ...

  4. iOS下载使用系统字体

    iOS下载使用系统字体 通用开发中一般使用系统默认的字体: 另外系统也提供了一些其他字体我们可以选择下载使用 1:在mac上打开 字体册 app 即可查找系统支持的字体,适用于ios上开发使用 从ma ...

  5. IOS第六天(3:scrollView 图片轮播器)

    IOS第六天(3:scrollView 图片轮播器) #import "HMViewController.h" #define kImageCount 5 @interface H ...

  6. iOS网络加载图片缓存策略之ASIDownloadCache缓存优化

    iOS网络加载图片缓存策略之ASIDownloadCache缓存优化   在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...

  7. ios里面如何压缩图片

    在iOS里面,压缩图片跟在其他环境里面差不多,都和累死, 就是对当前图片从新画图,制定一个尺寸的问题 UIImage* image = [UIImage imageNamed:@"cat.j ...

  8. 解决iOS中 tabBarItem设置图片(image+title切图在一起)时造成的图片向上偏移

    解决iOS中 tabBarItem设置图片(image+title切图在一起)时造成的图片向上偏移 解决办法1:设置tabBarItem的imageInsets属性 代码示例: childContro ...

  9. [置顶] iOS学习笔记47——图片异步加载之EGOImageLoading

    上次在<iOS学习笔记46——图片异步加载之SDWebImage>中介绍过一个开源的图片异步加载库,今天来介绍另外一个功能类似的EGOImageLoading,看名字知道,之前的一篇学习笔 ...

随机推荐

  1. linux 下wifi 功能的实现

    一.嵌入式linux四件套配置 1.网卡选型的依据: a.网卡不能乱选,否则没戏.网卡的依据是,kernel的menuconfig -> device drivers -> network ...

  2. JavaScript高级 引用类型(一)《JavaScript高级程序设计(第三版)》

    引用类型是一种数据结构.它也被称作类.有时也被称作 对象的定义. 对象 是某个特定引用类型的实例.   一.Object类型 表达式上下文(expression context):指能够返回一个值 语 ...

  3. Ubuntu16.04安装JDK

    转载请注明源出处:http://www.cnblogs.com/lighten/p/6105463.html 1.简单的安装方法 安装JDK的最简单方法应该就是使用apt-get来安装了,但是源一般是 ...

  4. Win 2003下IIS6+Mysql+php5.2  isapi搭建 升级php5.2到5.3测试 借助fastcgi实现

    Win 2003下IIS6+Mysql+php5.2  原环境isapi搭建 升级php5.2到5.3测试 借助fastcgi实现 操作如下 实验前准备:php-5.3.5-Win32-VC6-x86 ...

  5. UML类图常见关系总结

    Unified Modeling Language (UML)又称统一建模语言或标准建模语言. 在UML类图中,常见的有以下几种关系: 泛化(Generalization),  实现(Realizat ...

  6. Dev的DocumentManager添加窗体

    1.DocumentManager要设置自己的MdiParent属性 2.主窗体设置IsMidContainer为True 3.要生成的窗体设置MdiParent为主窗体 4.正常创建窗体,然后就可以 ...

  7. 学习c的第6天2

    #include <stdio.h> #include <math.h> int main() { float num; printf("请输入该生当月的消费额:\n ...

  8. 不复杂的Autofac注入

    private static void SetAutofacWebAPI() { var builder = new ContainerBuilder(); #region 配置注册方法 string ...

  9. 1.html5究竟是什么

    1.html5的起源,历史背景…… 按照一般的套路,我这里应该对html5的起源和发展历史,其优越性等大书特书一番.但既然你有意识地专门去找类似的文章,说明你早有相应的认识,就算没有,类似的东西网上也 ...

  10. Python Ogre Blender(转载)

    http://www.cppblog.com/Charlib/archive/2010/05/31/python_ogre_blender_1.html PyOgre入门以及如何使用Blender制作 ...