zoukankan      html  css  js  c++  java
  • iOS开发——闪光灯

      还是那句很欠揍的话,没啥难度,直接上代码。

    //

    //  ViewController.m

    //  Demo—闪光灯

    //

    //  Created by yyt on 16/4/21.

    //  Copyright © 2016年 yyt. All rights reserved.

    //

    #import "ViewController.h"

    #import <AVFoundation/AVFoundation.h>

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        UIButton *lightButton = [UIButton buttonWithType:UIButtonTypeCustom];

        lightButton.frame = CGRectMake(20, 160, self.view.bounds.size.width-40, 40);

        lightButton.backgroundColor = [UIColor orangeColor];

        [lightButton setTitle:@"open" forState:UIControlStateNormal];

        [lightButton setTitle:@"close" forState:UIControlStateSelected];

        [lightButton addTarget:self action:@selector(clickLightButton:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:lightButton];

    }

    - (void)clickLightButton:(UIButton *)sender {

        NSLog(@"闪光灯");

        sender.selected = !sender.selected;

        if (sender.selected) {

            [self turnTorchOn:YES];

        }

        else{

            [self turnTorchOn:NO];

        }

    }

    - (void)turnTorchOn:(BOOL)on

    {

        Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");

        if (captureDeviceClass != nil) {

            AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

            

            if ([device hasTorch] && [device hasFlash]){

                

                [device lockForConfiguration:nil];

                if (on) {

                    [device setTorchMode:AVCaptureTorchModeOn];

                    [device setFlashMode:AVCaptureFlashModeOn];

                    

                } else {

                    [device setTorchMode:AVCaptureTorchModeOff];

                    [device setFlashMode:AVCaptureFlashModeOff];

                }

                [device unlockForConfiguration];

            }

        }

    }

    @end

  • 相关阅读:
    使用JMeter进行RESTful API测试
    Jmeter中Websocket协议支持包的使用
    JMeterPlugins插件监听器学习-监听器
    Jmeter实现WebSocket协议的接口和性能测试方法
    使用JMeter创建数据库(Mysql)测试
    jmeter --JVM调优设置
    Android 开发者不得不面对的六个问题
    年底盘点之十大开源安全工具
    作为一个程序员怎么通过android开发赚钱
    新手做2D手游该用哪些工具?
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/5415900.html
Copyright © 2011-2022 走看看