博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS开发之小实例--使用UIImagePickerController创建一个简单的相机应用程序
阅读量:7093 次
发布时间:2019-06-28

本文共 2656 字,大约阅读时间需要 8 分钟。

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

 

网址:

 

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

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

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

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

1 #import "ViewController.h" 2  3 @interface ViewController () 
4 5 @property (strong, nonatomic) IBOutlet UIImageView *imageView; 6 7 @end 8 9 @implementation ViewController10 11 - (void)viewDidLoad {12 [super viewDidLoad];13 14 // 这段代码会自动判断当前设备是否有摄像机功能,如果没有,会弹窗提示15 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {16 17 UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"18 message:@"Device has no camera"19 delegate:nil20 cancelButtonTitle:@"OK"21 otherButtonTitles: nil];22 23 [myAlertView show];24 25 }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;33 34 [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;42 43 [self presentViewController:picker animated:YES completion:nil];44 }45 #pragma mark - 代理方法46 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
*)info{47 UIImage* chosenImage = info[UIImagePickerControllerEditedImage];48 self.imageView.image = chosenImage;49 50 [picker dismissViewControllerAnimated:YES completion:nil];51 }52 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{53 [picker dismissViewControllerAnimated:YES completion:nil];54 }55 56 @end

就这部分代码,别的没有了哦。

最后用你的真机测试使用一下哦。

 

转载地址:http://iuiql.baihongyu.com/

你可能感兴趣的文章
LINK : fatal error LNK1104
查看>>
WPF动态加载3D 放大-旋转-平移
查看>>
大型企业的渗透思路
查看>>
strace命令(收集整理,常看常新)
查看>>
Eclipse Console 加大显示的行数和禁止错误弹出
查看>>
$(document).height()与$(window).height()区别
查看>>
oracle字符集与客户端
查看>>
java线:辛格尔顿隐藏ThreadLocal实现线程数据共享
查看>>
MassTransit RabbitMQ 参考文档
查看>>
android 49 广播接收者中启动其他组件
查看>>
MySQL索引原理及慢查询优化
查看>>
JNI开发示例
查看>>
从netty-example分析Netty组件
查看>>
[经验]无线鼠标和无线键盘真的不能用了?——雷柏的重生之路~
查看>>
HTTP协议
查看>>
eclipse 创建maven 项目 动态web工程完整示例 maven 整合springmvc整合mybatis
查看>>
[Unit Testing] AngularJS Unit Testing - Karma
查看>>
Java NIO和IO的区别(转)
查看>>
Integer与int的区别(转)
查看>>
JavaScript 解决 onblur 与 onclick 冲突
查看>>