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