没什么好说的,注意一点,就是调用相机的时候需要真机测试,用模拟器的时候要加一个判断语句,不然崩溃
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
时间: 2024-11-13 06:55:11