这里主要用到了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:图片来源,有三个,照片流,图库,相机,区别和用法请参考代码
两个重要的代理方法
请参考代码,但是两个代理方法中都要写模态跳转,系统是不会自己跳回来的。