//
// ViewController.m
// IOS_0301_相册和相机
//
// Created by ma c on 16/3/1.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "ViewController.h" @interface ViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> @property (nonatomic, strong) UIImageView *chooseImgView;
@property (nonatomic, strong) UIButton *btnOpenCamera; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.chooseImgView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
self.chooseImgView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:self.chooseImgView];
self.btnOpenCamera = [UIButton buttonWithType:UIButtonTypeCustom];
[self.btnOpenCamera setFrame:CGRectMake(, , , )];
[self.btnOpenCamera setBackgroundColor:[UIColor redColor]];
[self.btnOpenCamera setTitle:@"相册相机" forState:UIControlStateNormal];
[self.view addSubview:self.btnOpenCamera]; [self.btnOpenCamera addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside];
}
/*
UIImagePickerController(图片拾取器)
能够根据不同的指令,选择开启相机或相册
ps:
1.使用图片拾取器时,实现两个协议
UIImagePickerControllerDelegate
UINavigationControllerDelegate
2.判断当前设备是否存在相机 */ - (void)openCamera
{
NSLog(@"打开相册"); UIActionSheet *actionSheeet = [[UIActionSheet alloc] initWithTitle:@"开启系统相机|相册" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相机" otherButtonTitles:@"相册", nil]; [actionSheeet showInView:self.view]; } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case :
{
NSLog(@"相机");
//设置图片拾取器
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
//设置代理
imgPicker.delegate = self;
//设置图片拾取器类型
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
//是否允许图片编辑
imgPicker.allowsEditing = YES;
//唤醒相机
[self presentViewController:imgPicker animated:YES completion:nil];
}
break;
case :
{
NSLog(@"相册");
//设置图片拾取器
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
//设置代理
imgPicker.delegate = self;
//设置图片拾取器类型
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//是否允许图片编辑
imgPicker.allowsEditing = YES;
//唤醒相机
[self presentViewController:imgPicker animated:YES completion:nil]; }
break; default:
break;
} } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
//完成获取图片的操作
NSLog(@"%@",info);
//获取编辑后的图片
UIImage *chooseImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];
NSString *imageName = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
NSLog(@"%@",imageName); self.chooseImgView.image = chooseImage; //将图片从系统相册中取出,并保存到沙河中
NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents"];
NSString *realPath = [homePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d",arc4random()%]]; NSLog(@"%@",realPath);
[UIImageJPEGRepresentation(chooseImage, 1.0f) writeToFile:realPath atomically:YES]; [self dismissViewControllerAnimated:YES completion:nil]; } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

最新文章

  1. android之电话拨号器
  2. Hadoop下面WordCount运行详解
  3. Web应用程序系统的多用户权限控制设计及实现-用户模块【7】
  4. Redis - list类型操作
  5. 对话框式Activity的设置
  6. RedHat 5 配置CentOS yum 更新源
  7. Epub2基础知识介绍
  8. redis 数据库实现
  9. TCP/IP中你不得不知的十大秘密
  10. JDownload: 一款可以从网络上下载文件的小程序第四篇(整体架构描述)
  11. Vue组件开发
  12. centos7只rsync+inotify
  13. [hadoop] kettle spoon 基础使用 (txt 内容抽取到excel中)
  14. 快速搭建完整zabbix3.4
  15. eclipse常用快捷键整理
  16. Django中使用bookstarp框架(4)
  17. vue案例 - vue-awesome-swiper实现h5滑动翻页效果
  18. Linux CentOS7系统探索
  19. fine ui使用笔记
  20. flutter控件之ListView滚动布局

热门文章

  1. python全栈开发从入门到放弃之socket并发编程之IO模型
  2. Mac下Jmeter快速安装与入门-模拟测试Post请求及设置Http头
  3. hdu1403 Longest Common Substring
  4. Winter-2-STL-B Brackets 解题报告及测试数据
  5. TOSCA自动化测试工具--openURL
  6. $PDB——Python调试利器详解
  7. Docker-docker制作镜像
  8. python3 使用opencv 画基本图形
  9. Java:延迟功能的Robot在Lunix系统上会报错
  10. Python3.x:常用基础语法