IOS开发调用系统相机和打开闪光灯
 
   今天给大家分享一下如何调用iphone的拍照功能和打开闪光灯,有些代码我也不太理解,很多是在网上借鉴其他人的。IOS有两种的拍照和视频的方式:1.直接使用UIImagePickerController,这个类提供了一个简单便捷的拍照与选择图片库里图片的功能。2.另一种是通过AVFoundation.framework框架完全自定义拍照的界面和选择图片库界面。我只做了第一种,就先给大家介绍第一种做法:
一、首先调用接口前,我们需要先判断当前设备是否支持UIImagePickerController,用isSourceTypeAvailable:来判断是否可用
二、查看符合的媒体类型,这个时候我们调用availableMediaTypeForSourceType:判断
在调用UIImagePickerController时我们需要加入他的两个代理方法:
UINavigationControllerDelegate和UIImagePickerControllerDelegate,在调用摄像头的时候还可以调闪光灯,一会代码里有。

要调用闪光灯需要先建一个AVCaptureSession类的实例对象:

[java] view plaincopy
  1. #import <UIKit/UIKit.h>
  2. //调用闪光灯调用框架
  3. #import <AVFoundation/AVFoundation.h>
  4. @interface CameraViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
  5. {
  6. AVCaptureSession * _AVSession;//调用闪光灯的时候创建的类
  7. }
  8. @property(nonatomic,retain)AVCaptureSession * AVSession;
  9. @end

在.m的- (void)viewDidLoad里建立4Button,Camera调用相机、Library调用图片库、flashlight打开闪光灯、close关闭闪光灯

[java] view plaincopy
  1. //打开相机
  2. -(void)addCarema
  3. {
  4. //判断是否可以打开相机,模拟器此功能无法使用
  5. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  6. UIImagePickerController * picker = [[UIImagePickerController alloc]init];
  7. picker.delegate = self;
  8. picker.allowsEditing = YES;  //是否可编辑
  9. //摄像头
  10. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  11. [self presentModalViewController:picker animated:YES];
  12. [picker release];
  13. }else{
  14. //如果没有提示用户
  15. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
  16. [alert show];
  17. }
  18. }

打开相机后,然后需要调用UIImagePickerControllerDelegate里的方法,拍摄完成后执行的方法和点击Cancel之后执行的方法:

[java] view plaincopy
  1. //拍摄完成后要执行的方法
  2. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  3. {
  4. //得到图片
  5. UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
  6. //图片存入相册
  7. UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
  8. [self dismissModalViewControllerAnimated:YES];
  9. }
  10. //点击Cancel按钮后执行方法
  11. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  12. {
  13. [self dismissModalViewControllerAnimated:YES];
  14. }

调用相机照片和保存到图片库已经完成。
接着介绍打开照片库:

[java] view plaincopy
  1. -(void)openPicLibrary
  2. {
  3. //相册是可以用模拟器打开的
  4. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
  5. UIImagePickerController * picker = [[UIImagePickerController alloc]init];
  6. picker.delegate = self;
  7. picker.allowsEditing = YES;//是否可以编辑
  8. //打开相册选择照片
  9. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  10. [self presentModalViewController:picker  animated:YES];
  11. [picker release];
  12. }else{
  13. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
  14. [alert show];
  15. }
  16. }
  17. //选中图片进入的代理方法
  18. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
  19. {
  20. [self dismissModalViewControllerAnimated:YES];
  21. }

调用闪光灯的代码

[java] view plaincopy
  1. -(void)openFlashlight
  2. {
  3. AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  4. if (device.torchMode == AVCaptureTorchModeOff) {
  5. //Create an AV session
  6. AVCaptureSession * session = [[AVCaptureSession alloc]init];
  7. // Create device input and add to current session
  8. AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
  9. [session addInput:input];
  10. // Create video output and add to current session
  11. AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc]init];
  12. [session addOutput:output];
  13. // Start session configuration
  14. [session beginConfiguration];
  15. [device lockForConfiguration:nil];
  16. // Set torch to on
  17. [device setTorchMode:AVCaptureTorchModeOn];
  18. [device unlockForConfiguration];
  19. [session commitConfiguration];
  20. // Start the session
  21. [session startRunning];
  22. // Keep the session around
  23. [self setAVSession:self.AVSession];
  24. [output release];
  25. }
  26. }
  27. -(void)closeFlashlight
  28. {
  29. [self.AVSession stopRunning];
  30. [self.AVSession release];
  31. }

IOS开发调用系统相机和打开闪光灯相关推荐

  1. Android开发 调用系统相机相册图片功能,解决小米手机拍照或者图片横竖相反问题,及小米手机相册图片路径问题

    Android开发 调用系统相机相册图片功能,解决小米手机拍照或者图片横竖相反问题,及小米手机相册图片路径问题 1.调用相机,兼容7.0 AndroidManifest配置 <providera ...

  2. ios开发调用系统自带的分享

    1.一般情况下提到分享,我们会想到去集成某些第三方的框架,例如很多第三方分享的集合例如友盟的,集成效果如下 这里只涉及到了常用的新浪.微信.及qq: 分享功能:三个平台都比较宽松,只有有appid,都 ...

  3. ios 调用系统相机为英文时的更改

    使用UIImagePickerViewController调用系统相机,当系统相机按钮为英文时. 在 info.plist 中添加 Localization native development re ...

  4. Android学习之调用系统相机实现拍照功能

    一.今天,来介绍如何调用系统自带的相机进行拍照,主要有以下2种实现的方式: 1.Camera应用程序包含了一个意图过滤器,即intent filter,它使得开发人员能够提供与Camera应用程序同等 ...

  5. android 相机拍照返回,Android6.0机型上调用系统相机拍照返回的resultCode值始终等于0的问题...

    版权声明:本文为博主原创文章,未经博主允许不得转载. 正常情况下调用系统相机拍照: 如果拍照后点击的是"确定"图标,返回的resultCode = -1(Activity.RESU ...

  6. Android7.0调用系统相机拍照、访问相册问题。

    关于Android7.0调用系统相机拍照.访问相册的一些问题: 在Android6.0中Google提出了动态申请权限的Api,调用相机拍照,访问SDcard等操作都需要先申请对应的权限如下: < ...

  7. 调用系统相机和相册出现闪退报错No Activity found to handle Intent

    调用系统相机和相册出现闪退报错No Activity found to handle Intent : 在开发安卓项目的时候遇到了一个问题,当手机调用系统相机和相册的时候会出现闪退的现象,根据报错常常 ...

  8. Android调用系统相机拍照

    参考: Taking Photos Simply FileProvider 项目地址(好多人找我要,我传到百度云啦,大家自取):链接:https://pan.baidu.com/s/1nWsoE0eS ...

  9. Android手机调用系统相机拍照、裁剪以及获取Url上传图片

    前言 最近一个人在公司搞独立开发,遇到问题只能自己解决,虽然过程比较坎坷,但是收获还是颇多的,一个人也是要坚强滴,最近弄用户的头像遇到了一些小问题,虽然上一款应用有头像上传,但是发现了其中的一些小问题 ...

最新文章

  1. 目标检测 - Tensorflow Object Detection API
  2. 20155339 Exp6 信息搜集与漏洞扫描
  3. 当城市实现完全自动驾驶,车该怎么开?人该怎么走?
  4. ajax的数据库,AJAX 数据库
  5. spring.profiles.active配置了没生效_一文带你彻底学会 Git Hooks 配置
  6. 【五校联考3day2】B
  7. 大端和小端的区别,以及如何判断一台机器是大端还是小端?
  8. Java ByteArrayOutputStream reset()方法及示例
  9. oracle 常用知识点整理
  10. HUE与YARN的集成
  11. OpenCV-Python实战(番外篇)——利用增强现实制作美颜挂件,让你的照片与众不同
  12. SmartBusinessDevFramework架构设计-2:结构图示
  13. freetds mysql_关于 freetds pymssql 的安装部署
  14. 【MATLAB】MATLAB 2017A 软件安装
  15. alc236黑苹果驱动_瑞昱 Realtek2.5G有线网卡 | RTL8125、RTL8156黑苹果驱动
  16. pyqtgraph 案例 002 Basic Plotting
  17. 每天老听别人念叨“算法+数据结构=程序”,知道是谁说的么?一起走进Pascal语言之父——图灵奖得主尼古拉斯·沃斯
  18. 云主机装黑果实践(5):重得到镜像和继续强化前置启动过程
  19. 删除域中的Exchange服务器
  20. root天猫,天猫盒子root

热门文章

  1. vue访问完整外部链接数据_【Excel小技巧】链接外部数据的五个方法
  2. 二十四、深入Python多进程multiprocessing模块
  3. linux用绝对路径执行mysql命令_Linux 相对路径和绝对路径的使用
  4. Transformer性能被高估?DeepMind动态评估模型的时间泛化能力
  5. 名额有限 | 邀你奔赴一场与太极图形开发者的约会
  6. 今日arXiv精选 | 13 篇 ICCV 2021 最新论文
  7. 旷视 AI 飞跃 | 研究生联合培养计划
  8. 贝叶斯神经网络对梯度攻击的鲁棒性
  9. 从“猿”到“金刚”,机器学习让你在职业生涯超进化!
  10. 回归理性 务实推进 迎接AI新时代 2018中国人工智能大会完美收官