As we said before, we need a delegate to deal with the user interaction with the camera or the photo library. In order to do that we have to conform to the UIImagePickerControllerDelegate protocol. Also, since we are going to present the camera (or the photo library) modally, we have to implement the UINavigationControllerDelegate protocol as well. Add both protocols to the AppViewController.h file:

1
@interface APPViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

When the user clicks (touch up inside) the “Take Photo” button, we have to create an UIImagePickerController and set its delegate to our AppViewController class. Also, we have to specify which kind of image picker we want to show, the camera with UIImagePickerControllerSourceTypeCamera, or a photo library with UIImagePickerControllerSourceTypePhotoLibrary; in this case we select the Camera. Once the picker has been crated, we have to present it modally with the method presentViewController.

Write the following takePhoto acton method:

1
2
3
4
5
6
7
8
9
10
- (IBAction)takePhoto:(UIButton *)sender {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    [self presentViewController:picker animated:YES completion:NULL];
    
}

Finally, we do the same for the selectPhoto action method, but changing the sourceType to UIImagePickerControllerSourceTypePhotoLibrary.

1
2
3
4
5
6
7
8
9
10
11
- (IBAction)selectPhoto:(UIButton *)sender {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:picker animated:YES completion:NULL];

}

最新文章

  1. 【转载】WEB前端开发规范文档
  2. js jquery 页面加载初始化方法
  3. AgileEAS.NET SOA 中间件平台.Net Socket通信框架-完整应用例子-在线聊天室系统-下载配置
  4. MFC 给对话框注册热键
  5. 【C语言入门教程】2.7 表达式
  6. ASP.NET 运行时详解 揭开请求过程神秘面纱
  7. Android应用与系统安全防御
  8. Myeclipse中把java代码导成UML类图
  9. js中的prototype和constructor
  10. 【Shell脚本学习14】Shell echo命令
  11. 用C#来查看电脑硬件和系统信息
  12. PO VO DAO DTO BO TO概念与区别
  13. leetcode第13题--Roman to Integer
  14. 一个Windows下线程池的实现(C++)
  15. Struts优缺点
  16. linux基础命令--rmdir 删除空目录
  17. SAML2.0 SP端处理
  18. [BZOJ 2759] 一个动态树好题
  19. centos 7 network.service control process exited
  20. 模块 import 与from

热门文章

  1. JDBC学习入门
  2. php 查看文档
  3. 【转】Apache配置中ProxyPassReverse指令的含义
  4. static的用法解析
  5. javaweb jsp页面上传excel文件
  6. javascript之String
  7. RTP 包格式 详细解析
  8. Paper.js - Paper.js
  9. cf#366....
  10. A Simple Problem with Integers(100棵树状数组)