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

  • 相关阅读:
    通用标签
    网页基础
    WCF---服务发布的步骤
    锁·——lock关键字详解
    C# 实现磁性窗体
    C#中的线程(三) 使用多线程
    C#中的线程(二) 线程同步基础
    C#中的线程(一)入门
    class A<T> where T:class 这个泛型类中的Where T:class什么意思
    OO真经——关于面向对象的哲学体系及科学体系的探讨(下)
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/5415900.html
Copyright © 2011-2022 走看看