原版的blog,转载请注明出处

blog.csdn.net/hello_hwc


前言:

新NSURLSession的UploadTask的,结果写那个Demo的时候想要写成拍照上传。然后就想到先写一个关于拍照的Demo吧。本文会先介绍下怎样使用系统提供的界面拍照和选择相冊,然后自己定义拍照界面。注意。本文使用的是UIImagePickerController,所以不能全然的自己定义。假设想要彻底的自己定义拍照。建议选择AV Foundation这个框架来做


Demo效果

进入系统的拍照界面


进入自己定义拍照界面


自己定义前置摄像头和后置摄像头切换动画-翻页


一 使用系统提供的界面拍照和相冊选择

第一步

保存一个UIImagePickerController的实例,然后适当的时候初始化始化。

Demo选择在viewDidLoad初始化。让当前类实现UIImagePickerControllerDelegate,UINavigationControllerDelegate两个代理

@property (strong,nonatomic)UIImagePickerController * imagePikerViewController;
//初始化
self.imagePikerViewController = [[UIImagePickerController alloc] init];
self.imagePikerViewController.delegate = self;//通过代理来传递拍照的图片
self.imagePikerViewController.allowsEditing = YES;//同意编辑

第二步,通过ActionSheet来让用户选择是拍照还是到相冊选择,然后模态的显示

[self presentViewController:self.imagePikerViewController animated:YES completion:NULL];

注意,要先推断相机是否可用,然后在进入相机(有可能相机坏了。或者在虚拟机上执行的)

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil
message: nil
preferredStyle:UIAlertControllerStyleActionSheet]; [alertController addAction: [UIAlertAction actionWithTitle: @"Take Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.imagePikerViewController animated:YES completion:NULL];
}else{
[self showAlertWithMessage:@"Camera is not available in this device or simulator"];
}
}]];
[alertController addAction: [UIAlertAction actionWithTitle: @"Choose Existing Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){
self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:self.imagePikerViewController animated:YES completion:NULL];
}
}]];
[alertController addAction: [UIAlertAction actionWithTitle: @"Cancel" style: UIAlertActionStyleCancel handler:nil]];
[self presentViewController: alertController animated: YES completion: nil];

第三部,代理函数处理拍照或者cancel事件

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage * image = info[UIImagePickerControllerEditedImage];
if (!image) {
image = info[UIImagePickerControllerOriginalImage];
}
self.imageview.image = image;
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:NULL];
}

二 自己定义拍照界面

UIImagePickerController的自己定义界面比較简单。通过设置cameraOverlayView这个属性为自己定义的View就能自己定义。

第一步 创建一个View

创建xib文件

拖拽控件。进行autoLayout,终于效果如图



把fileOwner设置成CustomTakePhotoViewController

第二步 在显示拍照界面之前,把UI设置成自己想要的,注意,把属性

showsCameraControls设置为NO,不让默认的界面出现。


self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePikerViewController.showsCameraControls = NO;
[[NSBundle mainBundle] loadNibNamed:@"CustomOverLayview" owner:self options:nil];
self.takePictureButton.layer.cornerRadius = 20;
self.takePictureButton.clipsToBounds = YES;
self.overlayView.frame = self.imagePikerViewController.cameraOverlayView.frame;
self.overlayView.backgroundColor = [UIColor clearColor];
self.imagePikerViewController.cameraOverlayView = self.overlayView;
self.overlayView = nil;
[self presentViewController:self.imagePikerViewController animated:YES completion:NULL];

第三步。为view上的控件加入动作

Segment Control负责切换前置摄像头和后置摄像头。为了流畅,在切换的时候显示动画。

- (IBAction)cameraSelect:(UISegmentedControl *)sender{
NSInteger index = sender.selectedSegmentIndex; if (index == 0) {
[UIView transitionWithView:self.imagePikerViewController.view duration:1.0 options:UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionCurlDown animations:^{
self.imagePikerViewController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
} completion:NULL];
}else{
[UIView transitionWithView:self.imagePikerViewController.view duration:1.0 options:UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionCurlUp animations:^{
[self.imagePikerViewController setCameraDevice:UIImagePickerControllerCameraDeviceRear];
} completion:NULL];
}
}

拍照的Button

- (IBAction)takePicture:(id)sender {
[self.imagePikerViewController takePicture];
}

取消的Button

- (IBAction)cancelTakePicture:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
}

第四步 在代理中处理图片

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage * image = info[UIImagePickerControllerEditedImage];
if (!image) {
image = info[UIImagePickerControllerOriginalImage];
}
self.imageview.image = image;
[self dismissViewControllerAnimated:YES completion:NULL];
}

注意,假设log输出

Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.

直接忽略就是了,没有不论什么影响。貌似是IOS 8的一个bug


下载链接

http://download.csdn.net/detail/hello_hwc/8553539

版权声明:本文博客原创文章。如需转载请注明出处

IOS SDK相机的详细解释/画廊(默认+他们的高清摄像头接口)的更多相关文章

  1. IOS 中得runloop 详细解释

    1.Runloop基础知识- 1.1 字面意思 a 运行循环 b 跑圈 - 1.2 基本作用(作用重大) a 保持程序的持续运行(ios程序为什么能一直活着不会死) b 处理app中的各种事件(比如触 ...

  2. 百度VS高德:LBS开发平台ios SDK对比评测

    随着iPhone6手机的热销,目前的iOS应用开发市场也迎来了全盛时期.据了解,目前市面上已有的iOS应用基本覆盖了购物.上门服务.用车服务.娱乐等行业.而在这些iOS应用中,内置LBS服务的应用占大 ...

  3. ios学习--TableView详细解释

    -.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; [DataTa ...

  4. iOS SDK具体解释之NSCopying协议

    原创blog,转载请注明出处 http://blog.csdn.net/hello_hwc?viewmode=contents 欢迎关注我的iOS SDK具体解释专栏 http://blog.csdn ...

  5. iOS SDK具体解释之UIDevice(系统版本号,设备型号...)

    原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 欢迎关注我的iOS SDK具体解释专栏 blog.csdn.net/column/details/huangwenchen ...

  6. 有趣 IOS 开展 - block 使用具体解释

    Block 它是iOS于4.0新的程序语法之后,于iOS SDK 4.0之后,block应用几乎无处不在. 在其他语言中也有类似的概念,称为闭包(closure),实例object C兄弟swift ...

  7. cmd 环境变量设置方法详细解释

    cmd设置环境变量可以方便我们bat脚本的运行,但是要注意的是变量只在当前的cmd窗口有作用(局部生效),如果想要设置持久的环境变量需要我们通过两种手段进行设置:一种是直接修改注册表,另一种是通过我的 ...

  8. .htaccess语法之RewriteCond与RewriteRule指令格式详细解释

    htaccess语法之RewriteCond与RewriteRule指令格式详细解释 (2012-11-09 18:09:08) 转载▼ 标签:  htaccess it 分类: 网络 上文htacc ...

  9. unity导出工程导入到iOS原生工程中详细步骤

    一直想抽空整理一下unity原生工程导入iOS原生工程中的详细步骤.做iOS+vuforia+unity开发这么长时间了.从最初的小小白到现在的小白.中间趟过了好多的坑.也有一些的小小收货.做一个喜欢 ...

随机推荐

  1. Velocity脚本新手教程

    从网络下的数据汇编 一.Velocity简介 Velocity它是Apache该公司的开源产品,它是一套基于Java语言模板引擎,背景可以非常灵活的数据与模板文件一起反对.他直言不讳地说:,人使用模板 ...

  2. JavaFX横幅类游戏开发 教训 游戏贴图

    上一节课,我们即将完成战旗Demo有了一个大概的了解.教训这,我们将学习绘制游戏地图. 由于JavaFX 2.2中添加了Canvas相关的功能,我们就能够使用Canvas来实现游戏绘制了. 游戏地图绘 ...

  3. ACdream: ACfun

    ACfun Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisti ...

  4. Machine Learning—Linear Regression

    Evernote的同步分享:Machine Learning-Linear Regression 版权声明:本文博客原创文章.博客,未经同意,不得转载.

  5. COM Interop

    1.MSDN上的文章:COM Interop教程 2.接口的三种类型:IDispatch.IUnknown和Dual 3.使用TlbImp来更灵活地自动生成RCW 4.托管事件基于委托,而非托管事件( ...

  6. 一起写2048(160行python代码)

    前言: Life is short ,you need python. --Bruce Eckel 我与2048的缘,不是缘于一个玩家.而是一次,一次,重新的ACM比赛.四月份校赛初赛,第一次碰到20 ...

  7. Serializable 作用

    Serializable 作用 序列化的attribute,是为了利用序列化的技术 准备用于序列化的对象必须设置 [System.Serializable] 标签,该标签指示一个类能够序列化. 便于在 ...

  8. EEPlat的元数据驱动的运行引擎

    EEPlat採用了元数据驱动的核心思想,因而EEPlat最重要的就是完好的元模型体系及高效灵活的解析运行引擎.EEPlat的运行引擎通过解析基于元模型的元数据,解释运行形成终于的业务系统. EEPla ...

  9. 于Eclipse传导C/C++配置方法开发(20140721新)

    Eclipse 它是一个开源.基于Java可扩展的开发平台. 在其自己的.它只是一个框架和一组服务.对于通过插件组件构建开发环境. --从百度百科的短语. 简单的说Eclipse 是免费的开源的Jav ...

  10. HDU 1195 Open the Lock (双宽搜索)

    意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...