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

  • 相关阅读:
    Fedora 14下安装使用rarlinux wang
    fedora 14 64位安装 flash player wang
    SQL中的 'N/A'含义
    Sql Sever中的replace()函数
    CompletableFuture的实际使用
    Asp.net检测系统是否装有.net环境
    JS与Asp.Net的传值
    JAVASCRIPT加密解密终级指南escape解密/eval加密/Encode加密原理
    在IIS上启用Gzip压缩(HTTP压缩)
    jquery批量上传图片
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/5415900.html
Copyright © 2011-2022 走看看