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,看名字知道,之前的一篇学习笔 ...
随机推荐
- ERS-2卫星
ERS-2卫星是欧洲空间局的第二颗资源遥感卫星,携带有雷达测高仪.综合孔径雷达等多种科学仪器,是一颗多任务.多学科的科学考察卫星.[1] ERS-1 ERS-2 欧空局分别于1991年和1995年发射 ...
- ASP.NET的SEO--- Global.asax和HttpModule中的RewritePath()方法
本系列目录 因为在网上搜到了很多这方面的文章,而且UrlRewrite中SEO中的重要性也在逐步下降,所以这一节我就写得简单一些.以下是几个重点: 1.UrlRewrite,顾名思义,只是针对URL进 ...
- windows live writer向cnblog发布文章设置(转)
Windows Live Writer是非常不错的一个日志发布工具,支持本地写文章,非常方便将word 中内容,包括图片进行处理,有点感觉相见恨晚. Live Writer最新版本下载地址是什么? h ...
- (笔记)angular选中变色
- py2.7+pyqt4开发端口检测工具
使用工具:python2.7,pyqt4,pyinstaller,pywin32 先贴代码 import sys from PyQt4 import QtGui,QtCore import threa ...
- XML内容作为String字符串读取报错
解决方案: 1.把头信息<?xml version='1.0' encoding='UTF-8'?>,但是内容会丢失部分: 2.用XmlDocument解析就OK. 正确代码: ...
- windows上修改路由表
1.查看电脑中的路由的命令: route print 2.修改“metric”,值越小权限越高: route add 0.0.0.0 mask 0.0.0.0 192.168.1.1 metric 5 ...
- Windows计算机功能Java源码
代码如下 import java.awt.Color; import java.awt.Container; import java.awt.GridLayout; import java.awt.e ...
- Jquery调用Webservice传递Json数组
Jquery由于提供的$.ajax强大方法,使得其调用webservice实现异步变得简单起来,可以在页面上传递Json字符串到Webservice中,Webservice方法进行业务处理后,返回Js ...
- linux之i2c子系统架构---总线驱动
编写i2c设备驱动(从设备)一般有两种方式: 1.用户自己编写独立的从设备驱动,应用程序直接使用即可. 2.linux内核内部已经实现了一个通用的设备驱动,利用通用设备驱动编写一个应用程序(用户态驱动 ...