iOS 取得单张系统图片
这里主要用到了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 取得单张系统图片的更多相关文章
- iOS UIButton文字和图片间距随意调整
代码地址如下:http://www.demodashi.com/demo/11606.html 前记 在开发中,我们经常会遇到这么一种情况,就是一个按钮上面有图片也有文字,但是往往设计并不是我们想要的 ...
- iOS 解决LaunchScreen中图片加载黑屏问题
iOS 解决LaunchScreen中图片加载黑屏问题 原文: http://blog.csdn.net/chengkaizone/article/details/50478045 iOS 解决Lau ...
- iOS根据Url 获取图片尺寸
iOS根据Url 获取图片尺寸 // 根据图片url获取图片尺寸 +(CGSize)getImageSizeWithURL:(id)imageURL { NSURL* URL = nil; if([i ...
- iOS下载使用系统字体
iOS下载使用系统字体 通用开发中一般使用系统默认的字体: 另外系统也提供了一些其他字体我们可以选择下载使用 1:在mac上打开 字体册 app 即可查找系统支持的字体,适用于ios上开发使用 从ma ...
- IOS第六天(3:scrollView 图片轮播器)
IOS第六天(3:scrollView 图片轮播器) #import "HMViewController.h" #define kImageCount 5 @interface H ...
- iOS网络加载图片缓存策略之ASIDownloadCache缓存优化
iOS网络加载图片缓存策略之ASIDownloadCache缓存优化 在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...
- ios里面如何压缩图片
在iOS里面,压缩图片跟在其他环境里面差不多,都和累死, 就是对当前图片从新画图,制定一个尺寸的问题 UIImage* image = [UIImage imageNamed:@"cat.j ...
- 解决iOS中 tabBarItem设置图片(image+title切图在一起)时造成的图片向上偏移
解决iOS中 tabBarItem设置图片(image+title切图在一起)时造成的图片向上偏移 解决办法1:设置tabBarItem的imageInsets属性 代码示例: childContro ...
- [置顶] iOS学习笔记47——图片异步加载之EGOImageLoading
上次在<iOS学习笔记46——图片异步加载之SDWebImage>中介绍过一个开源的图片异步加载库,今天来介绍另外一个功能类似的EGOImageLoading,看名字知道,之前的一篇学习笔 ...
随机推荐
- c# 利用 两个TREEVIEW控件完成TEENODE的鼠标拖动操作
功能说明: 我们有两个TREEVIEW控件——TREEVIEW1,TREEVIEW2.Treeview1内有三个NODE,Treeview2内有三个NODE.将Treeview1内的NODE拖动到Tr ...
- 对于不是特别擅长Photoshop的人来说,熟悉和运用Photoshop工具提供的各类便捷的快捷键,是有帮助的。
应用程序菜单快捷键之文件 应用程序菜单快捷键之编辑 应用程序菜单快捷键之图像图层 应用程序菜单快捷键 应用程序菜单快捷键之视图 Ctrl + H 取消参考线 调板菜单 ...
- 第四次java实验报告
20145306 实验四 java 开发基础 设计过程: 1.创建项目 2.选择activity_main.xml 3.显示自己的学号 4.双击改变字体大小 5.预览
- hadoop,hbase,pig安装
注意端口,办公网只能访问8000-9000的端口 pig的一些lib文件版本 /home/map/hadoop/lib下一些98.5的lib没删除
- svn不能提交 svn: is out of date; try updating
今天做项目合并的时候突然不能提交了,开始以为是和服务器的有修改冲突,然后更新一下发现还是不能提交,每次都报is out of date;还原文件 -> 修改 -> 提交 -> 仍然报 ...
- javaSE第二十三天
第二十三天 338 1.进程和线程的概述 338 2.多线程(理解) 339 (1)多线程:一个应用程序有多条执行路径 339 (2)Java程序的运行原理及JVM的启动是多线 ...
- 蘑菇街iOS客户端应用源码
蘑菇街iOS客户端应用源码 随着蘑菇街由导购向电商转型,蘑菇街自己的IM也应运而生,IM起初只是用于商家和买家之间沟通的工具.后面我们问自己,既然已经有了用于客服的IM,为什么不自己做一个IM,用于公 ...
- activiti搭建(五)BPMN介绍
转载请注明源地址:http://www.cnblogs.com/lighten/p/5931207.html 对于BPMN我也不是十分清楚,目前也只是因为对于Modeler中不熟悉的组件查询,来对这部 ...
- c#操作xml增删改查
1.首先新建一个xml文件(Root是我写上的) 2. 3.直接上代码,更直观 (1)初始化xml /// <summary> /// 初始化xml /// </summary> ...
- SqlServer存储过程学习笔记(增删改查)
* IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值. CREATE PROCEDURE [dbo].[PR_NewsAffiche_AddNewsEntity] ( ...