zoukankan      html  css  js  c++  java
  • iOS 直播-闪光灯的使用

    iOS 直播-闪光灯的使用


    应用场景是这样的,最近公司决定做一款直播类的软件.
    在开发中就遇到了不曾使用过的硬件功能-闪光灯.
    这篇博客将简单介绍一下闪光灯的使用.

    //
    //  ViewController.m
    //  iOS torch-test
    //
    //  Created by caoxu on 16/6/7.
    //  Copyright © 2016年 caoxu. All rights reserved.
    //
    
    #import "ViewController.h"
    #import <AVFoundation/AVFoundation.h>
    
    @interface ViewController ()
    
    @property (nonatomic, strong) AVCaptureSession * session;
    @property (nonatomic, strong) AVCaptureDevice * device;
    @property (nonatomic, strong) NSTimer * timer;
    
    @end
    
    @implementation ViewController
    
    #pragma mark <setter and getter>
    -(AVCaptureSession *)session
    {
        if(_session == nil)
        {
            _session = [[AVCaptureSession alloc] init];
        }
        return _session;
    }
    
    -(AVCaptureDevice *)device
    {
        if(_device == nil)
        {
            _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        }
        return _device;
    }
    
    #pragma mark <life cycle>
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        AVCaptureDeviceInput * input = [[AVCaptureDeviceInput alloc]initWithDevice:self.device error:nil];
        if ([self.session canAddInput:input]) {
            
            [self.session addInput:input];
            
        }
        
    
    }
    
    
    #pragma mark <method>
    - (IBAction)torchon:(id)sender {
        if([self.device hasTorch] && [self.device hasFlash])
        {
            if(self.device.torchMode == AVCaptureTorchModeOff)
            {
                [self.session beginConfiguration];
                [self.device lockForConfiguration:nil];
                [self.device setTorchMode:AVCaptureTorchModeOn];
                [self.device setFlashMode:AVCaptureFlashModeOn];
                [self.device unlockForConfiguration];
                [self.session commitConfiguration];
            }
        }
        
        [self.session startRunning];
        
    }
    - (IBAction)torchoff:(id)sender {
        
        [self.session beginConfiguration];
        [self.device lockForConfiguration:nil];
        
        if(self.device.torchMode == AVCaptureTorchModeOn)
        {
            [self.device setTorchMode:AVCaptureTorchModeOff];
            [self.device setFlashMode:AVCaptureFlashModeOff];
        }
        
        [self.device unlockForConfiguration];
        [self.session commitConfiguration];
        [self.session stopRunning];
        
    }
    
    
    @end
    
    
  • 相关阅读:
    java.lang.NoClassDefFoundError异常处理
    CMS之promotion failed&concurrent mode failure
    jvm 内存,线程,gc分析
    spring 参数校验
    常用的正则表达式
    《深入理解java虚拟机-高效并发》读书笔记
    ConcurrentHashMap源码分析
    web前端性能调优(二)
    由自动装箱和拆箱引发我看Integer源码
    阅读《effective java-第17条》遇到的问题解决与分享
  • 原文地址:https://www.cnblogs.com/xubaoaichiyu/p/5567333.html
Copyright © 2011-2022 走看看