码迷,mamicode.com
首页 > 其他好文 > 详细

打开闪光灯的代码

时间:2016-04-06 18:16:58      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

1.比较常用,也比较简单,因此粘贴到这里

 1 - (void)onFlash:(UIButton *)sender{
 2     
 3     AVCaptureDevice *avDevice = self.device;
 4     if (![avDevice hasFlash] || ![avDevice hasTorch]) {
 5         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"打开摄像头失败!请检测您的摄像头!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
 6         [alert show];
 7         return;
 8     }
 9 
10     sender.selected = !sender.selected;
11     [self openFlashLight:sender];
12 }
13 
14 - (void)openFlashLight:(UIButton *)sender
15 {
16     AVCaptureSession *avSession = self.captureSession;
17     AVCaptureDevice *avDevice = self.device;
18     if (sender.selected) {
19         if (avDevice.torchMode == AVCaptureTorchModeOff) {
20             [avSession beginConfiguration];
21             [avDevice lockForConfiguration:nil];
22             [avDevice setTorchMode:AVCaptureTorchModeOn];
23             [avDevice setFlashMode:AVCaptureFlashModeOn];
24             [avDevice unlockForConfiguration];
25         }
26     }
27     else
28     {
29         if (avDevice.torchMode == AVCaptureTorchModeOn) {
30             [avSession beginConfiguration];
31             [avDevice lockForConfiguration:nil];
32             [avDevice setTorchMode:AVCaptureTorchModeOff];
33             [avDevice setFlashMode:AVCaptureFlashModeOff];
34             [avDevice unlockForConfiguration];
35         }
36     }
37 }

 

打开闪光灯的代码

标签:

原文地址:http://www.cnblogs.com/lz465350/p/5360340.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!