设为首页 加入收藏

TOP

iOS 直播-闪光灯的使用
2017-10-13 10:23:49 】 浏览:4775
Tags: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
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Runtime之方法交换 下一篇iOS FFmpeg 优秀博客(资源)集锦

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目