在AppStore 经常会看到 使用闪光灯的应用,一般都是一些做为照明的应用,今天无聊,自己就动手做了一个手电筒demo,其实很简单,使用闪光灯,需要导入 AVFoundation.framework.
主要的类:AVCaptureSession , AVCaptureDevice , AVCaptureDeviceInput .
实现打开和关闭闪光灯只要改变AVCaputreDevice类的torchMode 和 flashMode 属性的值就可以了。
属性torchMode 三种类型zhi
enum {
AVCaptureTorchModeOff = 0,
AVCaptureTorchModeOn = 1,
AVCaptureTorchModeAuto = 2,
};
typedef NSInteger AVCaptureTorchMode;
属性flashMode 三种类型值:
enum {
AVCaptureFlashModeOff = 0,
AVCaptureFlashModeOn = 1,
AVCaptureFlashModeAuto = 2
};
typedef NSInteger AVCaptureFlashMode;
这个demo 实现的功能:打开闪光灯,关闭闪光灯,sos模式闪光灯。
主要代码:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISwitch *swith;
@property (weak, nonatomic) IBOutlet UIButton *sosStartBtn;
@property (weak, nonatomic) IBOutlet UIButton *sosStopBtn;
- (IBAction)changeMode:(UISwitch *)sender;
- (IBAction)startSOSMode:(id)sender;
- (IBAction)stopSOSMode:(id)sender;
@end
ViewController.m
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic,strong) AVCaptureSession * captureSession;
@property (nonatomic,strong) AVCaptureDevice * captureDevice;
@property (nonatomic,strong) NSTimer * sosTimer;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView * bgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
[bgView setImage:[UIImage imageNamed:@"iCtrl_Home_Bg@2x.png"]];
[self.view insertSubview:bgView atIndex:0];
[self.sosStartBtn setImage:[UIImage imageNamed:@"iCtrl_Home_Button_Play@2x.png"] forState:UIControlStateDisabled];
[self.sosStopBtn setImage:[UIImage imageNamed:@"iCtrl_Home_Button_Pause@2x.png"] forState:UIControlStateDisabled];
AVCaptureDeviceInput * deviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:self.captureDevice error:nil];
[self.captureSession addInput:deviceInput];
// AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc] init];
//
// [self.captureSession addOutput:output];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//普通开关模式
- (IBAction)changeMode:(UISwitch *)sender {
if(sender.on)
{
//打开闪光灯
NSLog(@"ON");
self.sosStartBtn.enabled = NO;
self.sosStopBtn.enabled = NO;
if([self.captureDevice hasTorch] && [self.captureDevice hasFlash])
{
if(self.captureDevice.torchMode == AVCaptureTorchModeOff)
{
[self.captureSession beginConfiguration];
[self.captureDevice lockForConfiguration:nil];
[self.captureDevice setTorchMode:AVCaptureTorchModeOn];
[self.captureDevice setFlashMode:AVCaptureFlashModeOn];
[self.captureDevice unlockForConfiguration];
[self.captureSession commitConfiguration];
}
}
[self.captureSession startRunning];
}
else
{
//关闭闪光灯
NSLog(@"Off");
self.sosStartBtn.enabled = YES;
self.sosStopBtn.enabled = YES;
[self.captureSession beginConfiguration];
[self.captureDevice lockForConfiguration:nil];
if(self.captureDevice.torchMode == AVCaptureTorchModeOn)
{
[self.captureDevice setTorchMode:AVCaptureTorchModeOff];
[self.captureDevice setFlashMode:AVCaptureFlashModeOff];
}
[self.captureDevice unlockForConfiguration];
[self.captureSession commitConfiguration];
[self.captureSession stopRunning];
}
}
//开始sos模式
- (IBAction)startSOSMode:(id)sender {
// [self performSelectorOnMainThread:@selector(sosTimer:) withObject:nil waitUntilDone:YES];
self.swith.enabled = NO;
self.sosStartBtn.hidden = YES;
self.sosStopBtn.hidden = NO;
if(self.sosTimer != nil)
{
[self.sosTimer invalidate];
self.sosTimer = nil;
}
//使用定时器
self.sosTimer = [NSTimer scheduledTimerWithTimeInterval:300.0/1000.0f target:self selector:@selector(sosTimer:) userInfo:nil repeats:YES];
[self.sosTimer fire];
}
//停止sos模式
- (IBAction)stopSOSMode:(id)sender {
self.swith.enabled = YES;
self.sosStopBtn.hidden = YES;
self.sosStartBtn.hidden = NO;
[self.sosTimer invalidate];
self.sosTimer = nil;
//当你取消定时器时,有可能闪光灯还亮着,这里要判断一下,如果还亮着,就关闭它
if(self.captureDevice.torchMode == AVCaptureFlashModeOn)
{
[self.captureSession beginConfiguration];
[self.captureDevice lockForConfiguration:nil];
[self.captureDevice setTorchMode:AVCaptureTorchModeOff];
[self.captureDevice setFlashMode:AVCaptureFlashModeOff];
[self.captureDevice unlockForConfiguration];
[self.captureSession commitConfiguration];
[self.captureSession stopRunning];
}
}
//定时执行方法
-(void)sosTimer:(NSTimer *)timer
{
NSLog(@"SOS");
[self.captureSession beginConfiguration];
[self.captureDevice lockForConfiguration:nil];
//判断闪光灯是否亮着
if(self.captureDevice.torchMode == AVCaptureTorchModeOff)
{
//打开闪光灯
[self.captureDevice setTorchMode:AVCaptureTorchModeOn];
[self.captureDevice setFlashMode:AVCaptureFlashModeOn];
}
else
{
//关闭闪光灯
[self.captureDevice setTorchMode:AVCaptureTorchModeOff];
[self.captureDevice setFlashMode:AVCaptureFlashModeOff];
}
[self.captureDevice unlockForConfiguration];
[self.captureSession commitConfiguration];
if(self.captureDevice.torchMode == AVCaptureTorchModeOff)
{
[self.captureSession startRunning];
}
else
{
[self.captureSession stopRunning];
}
}
-(AVCaptureSession *)captureSesion
{
if(_captureSession == nil)
{
_captureSession = [[AVCaptureSession alloc] init];
}
return _captureSession;
}
-(AVCaptureDevice *)captureDevice
{
if(_captureDevice == nil)
{
_captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
return _captureDevice;
}
@end
xib 布局如图:
效果图:
我已经把demo上传到百度网盘,可以下载看看:http://pan.baidu.com/share/link?shareid=3008907114&uk=4280928907