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