使用闪光灯需要引入AVFoundation框架,使用的类是AVCaptureSession , AVCaptureDevice , AVCaptureDeviceInput,然后在通过设置硬件属性,打开或关闭手电筒或flash就可以实现闪光灯效果,有demo,demo注释很清楚,肯定能让你初步明白怎么打开手电筒,demo链接http://download.csdn.net/download/lcg0412/6832689
一些设置属性:
enum {
AVCaptureDevicePositionB
AVCaptureDevicePositionF
};
typedef NSInteger AVCaptureDevicePosition;
闪光灯开关
enum {
AVCaptureFlashModeOff
AVCaptureFlashModeOn
AVCaptureFlashModeAuto = 2
};
typedef NSInteger AVCaptureFlashMode;
手电筒开关
enum {
AVCaptureTorchModeOff
AVCaptureTorchModeOn
AVCaptureTorchModeAuto = 2,
};
typedef NSInteger AVCaptureTorchMode;
焦距调整
enum {
AVCaptureFocusModeLocked
AVCaptureFocusModeAutoFo
AVCaptureFocusModeContin
};
typedef NSInteger AVCaptureFocusMode;
曝光量调节
enum {
AVCaptureExposureModeLoc
AVCaptureExposureModeAut
AVCaptureExposureModeCon
};
typedef NSInteger AVCaptureExposureMode;
白平衡
enum {
AVCaptureWhiteBalanceMod
AVCaptureWhiteBalanceMod
};
typedef NSInteger AVCaptureWhiteBalanceMod
//提醒 - (void)alertView:(NSString *)text{ UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:text message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil]; [alertView show]; [alertView performSelector:@selector(dismissWithClickedButtonIndex:animated:) withObject:@[@0, @YES] afterDelay:1]; } //定时器,打开或关闭手电筒 - (void)timerAction{ if(self.captureDevice.torchMode == AVCaptureTorchModeOn){ [self openTorch:AVCaptureTorchModeOff]; } else{ [self openTorch:AVCaptureTorchModeOn]; } } //打开或关闭手电筒 - (void)openTorch:(AVCaptureTorchMode)mode{ if(self.captureDevice.torchMode != mode){ //配置会话层 [self.captureSession beginConfiguration]; //对设备进行锁定操作 [self.captureDevice lockForConfiguration:NULL]; //打开手电筒 [self.captureDevice setTorchMode:mode]; //解锁 [self.captureDevice unlockForConfiguration]; //确认配置 [self.captureSession commitConfiguration]; } } //闪光灯效果 - (IBAction)flashAction:(UISwitch *)sender { if(![self.captureDevice hasTorch]){ [self alertView:@"亲,模拟器上没有手电筒哦。"]; return; } if(sender.on){ self.torchButton.enabled = NO; if(self.timer == nil){ self.timer = [NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; } [self.timer fire]; } else{ self.torchButton.enabled = YES; [self.timer invalidate]; self.timer = nil; } } //手电筒效果 - (IBAction)torchOpen:(UISwitch *)sender { if(![self.captureDevice hasTorch]){ [self alertView:@"亲,模拟器上没有手电筒哦。"]; return; } if(sender.on){ self.flashButton.enabled = NO; [self openTorch:AVCaptureTorchModeOn]; } else{ self.flashButton.enabled = YES; [self openTorch:AVCaptureTorchModeOff]; } } - (AVCaptureDevice *)captureDevice{ if(_captureDevice == nil){ _captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; } return _captureDevice; } - (AVCaptureDeviceInput *)captureDeviceInput{ if(_captureDeviceInput == nil){ _captureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self captureDevice] error:NULL]; } return _captureDeviceInput; } - (AVCaptureSession *)captureSession{ if(_captureSession == nil){ _captureSession = [[AVCaptureSession alloc] init]; [_captureSession addInput:[self captureDeviceInput]]; } return _captureSession; }