前言:本篇博文是本人阅读国外的IOS Programming Tutorial的一篇入门文章的学习过程总结,难度不大,因为是入门。主要是入门UIImagePickerController这个控制器,那么这个控制器是干嘛的呢?就是调用设备摄像机功能用的。到后面可能需要您在真机上测试,因为iPhone模拟器无法支持摄像机功能,运行测试会崩溃的哦。
网址:http://www.appcoda.com/ios-programming-camera-iphone-app
其实我就按照这篇博文的讲解过程,自己做了一遍,也敲了一遍代码,很快就熟悉了这个UIImagePickerController是啥玩意了。
1、首先简单的创建一个工程,然后在storyboard和对应的.m文件中添加相关的代码,这个简明教程没有使用自动布局,不多说,看图识字:
2、下面是这个ViewController.m的完整实现:
1
#import
"
ViewController.h
"
2
3
@interface ViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
4 5 @property (strong, nonatomic) IBOutlet UIImageView *imageView;
6 7@end 8 9@implementation ViewController
1011 - (void)viewDidLoad {
12 [super viewDidLoad];
1314// 这段代码会自动判断当前设备是否有摄像机功能,如果没有,会弹窗提示15if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
1617 UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"18 message:@"Device has no camera"19delegate:nil
20 cancelButtonTitle:@"OK"21 otherButtonTitles: nil];
2223 [myAlertView show];
2425 }
26}
27 - (IBAction)takePhotot:(UIButton *)sender {
28// 创建UIImagePickerController控制器对象29 UIImagePickerController *picker = [[UIImagePickerController alloc] init];
30 picker.delegate = self;
31 picker.allowsEditing = YES;
32 picker.sourceType = UIImagePickerControllerSourceTypeCamera;
3334 [self presentViewController:picker animated:YES completion:nil];
35}
36 - (IBAction)selectPhoto:(UIButton *)sender {
37// 创建UIImagePickerController控制器对象38 UIImagePickerController *picker = [[UIImagePickerController alloc] init];
39 picker.delegate = self;
40 picker.allowsEditing = YES;
41 picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
4243 [self presentViewController:picker animated:YES completion:nil];
44}
45#pragma mark - 代理方法
46 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
47 UIImage* chosenImage = info[UIImagePickerControllerEditedImage];
48 self.imageView.image = chosenImage;
4950 [picker dismissViewControllerAnimated:YES completion:nil];
51}
52 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
53 [picker dismissViewControllerAnimated:YES completion:nil];
54}
5556@end
就这部分代码,别的没有了哦。
最后用你的真机测试使用一下哦。
原文:http://www.cnblogs.com/goodboy-heyang/p/5403947.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!