zoukankan      html  css  js  c++  java
  • 打开闪光灯的代码

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

    2.调用了AVCaptureSession对象的beginConfiguration方法后,勿忘调用commitConfiguration方法,有始有终.

    3.1 在程序①返回时②失去焦点时③扫描成功时千万不要忘记关闭闪光灯,并关闭 session 对象,否则极易崩溃.

     1 - (void)onFlash{
     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     [self openFlashLight];
    11 }
    12 
    13 - (void)openFlashLight
    14 {
    15     UIButton *sender = self.buttonFlash;
    16     sender.selected = !sender.selected;
    17     if (sender.selected) {
    18         [self openFlash];
    19     } else{
    20         [self shutdownFlash];
    21     }
    22 }
    23 
    24 - (void)openFlash{
    25     
    26     AVCaptureDevice *avDevice = self.device;
    27     AVCaptureSession *avSession = self.captureSession;
    28     if (avDevice.torchMode == AVCaptureTorchModeOff) {
    29         [avSession beginConfiguration];
    30         [avDevice lockForConfiguration:nil];
    31         [avDevice setTorchMode:AVCaptureTorchModeOn];
    32         [avDevice setFlashMode:AVCaptureFlashModeOn];
    33         [avDevice unlockForConfiguration];
    34         [avSession commitConfiguration];
    35         self.buttonFlash.selected = YES;
    36     }
    37 }
    38 
    39 - (void)shutdownFlash{
    40     
    41     AVCaptureDevice *avDevice = self.device;
    42     AVCaptureSession *avSession = self.captureSession;
    43     if (avDevice.torchMode == AVCaptureTorchModeOn) {
    44         [avSession beginConfiguration];
    45         [avDevice lockForConfiguration:nil];
    46         [avDevice setTorchMode:AVCaptureTorchModeOff];
    47         [avDevice setFlashMode:AVCaptureFlashModeOff];
    48         [avDevice unlockForConfiguration];
    49         [avSession commitConfiguration];
    50         self.buttonFlash.selected = NO;
    51     }
    52 }

    3.2以下是退出时的处理代码,不能丢!

     1 - (void)endScan{
     2     [self stopAnimation];
     3     [self shutdownFlash];
     4     [self stopSeesion];
     5 }
     6 
     7 - (void)stopAnimation{
     8     [self.imageViewShockWave.layer removeAllAnimations];
     9 }
    10 
    11 - (void)shutdownFlash{
    12     
    13     AVCaptureDevice *avDevice = self.device;
    14     AVCaptureSession *avSession = self.captureSession;
    15     if (avDevice.torchMode == AVCaptureTorchModeOn) {
    16         [avSession beginConfiguration];
    17         [avDevice lockForConfiguration:nil];
    18         [avDevice setTorchMode:AVCaptureTorchModeOff];
    19         [avDevice setFlashMode:AVCaptureFlashModeOff];
    20         [avDevice unlockForConfiguration];
    21         [avSession commitConfiguration];
    22         self.buttonFlash.selected = NO;
    23     }
    24 }
    25 
    26 - (void)stopSeesion{
    27     if (_captureSession) {
    28         [_captureSession stopRunning];
    29     }
    30 }
  • 相关阅读:
    在LINQTOSQL中实现“级联删除”的方法
    “BindingNavigator”如何在删除前弹出确认框?
    OOP设计思考——究竟是继承自普通类,还是继承自抽象类?
    ASP.NET控件为什么无法使用结构?
    如何消除Web自定义控件的“自生成”复合属性的冗余类名称?
    用C#动态输出js单引号问题
    关于ready和load方法作用于不同情况下的比较
    关于CodeSign error : Certificate identity 'iPhone Distribution *** : ...问题
    [iOS]Xcode4/iOS5调试UncaughtException崩溃First throw call stack不打印方法名的解决方案
    Lion版本Mac OS下查看iPhone Simulator目录
  • 原文地址:https://www.cnblogs.com/lz465350/p/5360340.html
Copyright © 2011-2022 走看看