调用相机、相册

没什么好说的,注意一点,就是调用相机的时候需要真机测试,用模拟器的时候要加一个判断语句,不然崩溃

 1 #import "RootViewController.h"
 2 #import "RootView.h"
 3 
 4 @interface RootViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
 5 
 6 @property (nonatomic, strong) RootView *rootView;
 7 @end
 8 
 9 @implementation RootViewController
10 
11 - (void)loadView {
12     self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
13     self.view = self.rootView;
14 }
15 
16 
17 
18 - (void)viewDidLoad {
19     [super viewDidLoad];
20     // Do any additional setup after loading the view.
21     
22     
23     // 添加点击事件
24     [self.rootView.cameraButton addTarget:self action:@selector(cameraButtonClick:) forControlEvents:UIControlEventTouchUpInside];
25     
26     [self.rootView.photoButton addTarget:self action:@selector(photoButtonClick:) forControlEvents:UIControlEventTouchUpInside];
27 }
28 
29 
30 
31 #pragma mark - 实现点击事件
32 // 调用相机
33 - (void)cameraButtonClick:(UIButton *)sender {
34     
35     // 检测可用源
36     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
37         // 1.创建对象并初始化
38         UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
39         
40         // 2.添加数据来源
41         imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
42         
43         // 3.设置代理
44         imgPicker.delegate = self;
45         
46         // 4.是否允许编译
47         imgPicker.allowsEditing = YES;
48         
49         // 5.实现页面跳转
50         [self presentViewController:imgPicker animated:YES completion:nil];
51     } else {
52         [sender setTitle:@"NO" forState:UIControlStateNormal];
53     }
54 }
55 
56 // 实现代理方法
57 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
58     
59     [self dismissViewControllerAnimated:YES completion:nil];
60 }
61 
62 
63 
64 // 调用相册
65 - (void)photoButtonClick:(UIButton *)sender {
66     
67     // 1.创建对象并初始化
68     UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
69     
70     // 2.添加数据来源
71     imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
72     
73     // 3.设置代理
74     imgPicker.delegate = self;
75     
76     // 4.是否允许编译
77     imgPicker.allowsEditing = YES;
78     
79     // 5.实现页面跳转
80     [self presentViewController:imgPicker animated:YES completion:nil];
81 }
82 
83 @end

 

转载于:https://www.cnblogs.com/zhizunbao/p/5401436.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值