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,看名字知道,之前的一篇学习笔 ...
随机推荐
- python发邮件遇到的端口号问题
在学习使用python发邮件的过程中, 遇到了一个问题:由于测试的时候使用的是QQ邮箱,要求必须使用SSL/TLS加密,所以有了下面的代码, from email.mime.text import M ...
- Hbase rest方式获取指定key范围内的值
代码如下: <?php class Monitor_Hbase{ private $rest_host = "http://10.99.90.39:8130/";//rest ...
- 中间人攻击破解HTTPS传输内容
最近App安全受到不小的關注,有人問我,說某某App不安全,究竟是真的還假的啊...所謂有被攻擊的風險,是不是危言聳聽,只是為了嚇人來著的? 現在就來為各位說明一下,是怎麼個不安全法.就來說說攻擊是怎 ...
- mysql中union与union all的区别
当查询表结构完全相同的多张表的数据时: 1.当查询条件完全相同且不包括主键,此时用union查询会过滤掉查询出的重复的记录,及漏查记录:使用union all进行查询,则会查出所有的符合条件的记录,保 ...
- crontab的使用说明
网上瞎转载的,仅供参考 名称 : crontab 使用权限 : 所有使用者 使用方式 : crontab file [-u user]-用指定的文件替代目前的crontab. crontab-[-u ...
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(四)-- Middleware
本文记录了Asp.Net管道模型和Asp.Net Core的Middleware模型的对比,并在上一篇的基础上增加Middleware功能支持. 在演示Middleware功能之前,先要了解一下Asp ...
- 网络流量监控工具iftop
#-------------------网络流量监控工具iftop---------------##! /bin/sh #1.首先安装依赖关系包yum install flex byacc libpc ...
- Regarding the %EDIT table
%EDITTABLE is field in the work record DERIVED. This field is generally used a prompt table for vari ...
- “requireJs前传”之为什么要用前端模块化?
对于没有接触过后台的前端同学想要理解模块化是很困难的,鉴于未来的趋势,以下是我转载的一篇文章,希望对大家有用! 特此声明:转载文章,不喜勿喷.和谐前端,世界和平!0.0 模块的写法 随着网站逐渐变成” ...
- wordpress设置导航栏
设置导航栏,首先你要设置你的导航分类.登陆后台---文章---分类目录,首先在这里输入你要写入导航的标题. 设置好后点击---外观---菜单这个地方就可以具体的设置导航的排序和下拉等二级