前言:本篇博文是本人阅读国外的IOS Programming Tutorial的一篇入门文章的学习过程总结,难度不大,因为是入门。主要是入门UIImagePickerController这个控制器,那么这个控制器是干嘛的呢?就是调用设备摄像机功能用的。到后面可能需要您在真机上测试,因为iPhone模拟器无法支持摄像机功能,运行测试会崩溃的哦。

网址:http://www.appcoda.com/ios-programming-camera-iphone-app

其实我就按照这篇博文的讲解过程,自己做了一遍,也敲了一遍代码,很快就熟悉了这个UIImagePickerController是啥玩意了。

为了帮助您了解的UIImagePickerController的使用,我们将构建一个简单的摄像头应用程序。该示例应用程序非常简单:我们将有一个主窗口有一个大的UIImageView显示选中的照片,和两个按钮:一个用于拍摄新照片,而另一个选择从照片库中的照片。

1、首先简单的创建一个工程,然后在storyboard和对应的.m文件中添加相关的代码,这个简明教程没有使用自动布局,不多说,看图识字:

2、下面是这个ViewController.m的完整实现:

 #import "ViewController.h"

 @interface ViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>

 @property (strong, nonatomic) IBOutlet UIImageView *imageView;

 @end

 @implementation ViewController

 - (void)viewDidLoad {
[super viewDidLoad]; // 这段代码会自动判断当前设备是否有摄像机功能,如果没有,会弹窗提示
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device has no camera"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil]; [myAlertView show]; }
}
- (IBAction)takePhotot:(UIButton *)sender {
// 创建UIImagePickerController控制器对象
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:picker animated:YES completion:nil];
}
- (IBAction)selectPhoto:(UIButton *)sender {
// 创建UIImagePickerController控制器对象
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:picker animated:YES completion:nil];
}
#pragma mark - 代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage* chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage; [picker dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
} @end

就这部分代码,别的没有了哦。
最后用你的真机测试使用一下哦。

最新文章

  1. TOP 和 OFFSET 筛选(转)
  2. 批量设置select为默认项
  3. 【MVC 4】6.SportsSore:导航
  4. 实践:VIM深入研究(20135301 &amp;&amp; 20135337)
  5. python计算非内置数据类型占用内存
  6. lintcode:线段树的修改
  7. HDOJ-ACM1004(JAVA)
  8. VirtualBox设置共享文件夹和镜像访问的方法
  9. raw和字符串的转换。
  10. 不得不看的JVM内存管理
  11. Linux vps无法发送邮件
  12. html的简单表单制作...day5 php
  13. Eclipse识别不了jsp中的${pageContxt.request.contextPath }
  14. [转] js实现对图片的二进制流md5计算
  15. mysql找到数据的存储位置
  16. JS的防抖,节流,柯里化和反柯里化
  17. 解决 MySQL 比如我要拉取一个消息表中用户id为1的前10条最新数据
  18. 测试word发表博客
  19. TOJ1398正方形的编成 或者 POJ2362
  20. SpringDataRedis java.net.UnknownHostException: 127.0.0.1 错误

热门文章

  1. .NET Framework介绍
  2. html5学习笔记6-- canvas
  3. 纯css的防止图片撑破页面的代码图片会自动按比例缩小
  4. JSP读取My SQL数据乱码问题的解决
  5. HDU 1141---Brackets Sequence(区间DP)
  6. 2016暑假多校联合---Counting Intersections
  7. Spark集群 + Akka + Kafka + Scala 开发(1) : 配置开发环境
  8. 计划任务crontab
  9. Web Service代理类生成工具
  10. 【转】超实用PHP函数总结整理