调用设备闪光灯,使用“AVFoundation.framework”框架里的“AVCaptureDevice”来调用设备闪光灯。
详细操作步骤如下:
步骤1导入“AVFoundation.framework”框架
步骤2引入头文件“<AVFoundation/AVFoundation.h>”,即 #import <AVFoundation/AVFoundation.h>
步骤3初始化AVCaptureDevice实例
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
步骤4异常判断,判断设备是否支持闪光灯功能
4-1 [captureDevice hasTorch]
4-2 [captureDevice hasFlash]
步骤5闪光灯的打开或关闭
[captureDevice lockForConfiguration:nil];
[captureDevice setTorchMode: AVCaptureTorchModeOff]; //打开或关闭
[captureDevice unlockForConfiguration];
详细代码:
.h文件
@interface FreshlightManager : NSObject
- (void)showFreshlight;
@end
.m文件
#import <AVFoundation/AVFoundation.h>
@implementation FreshlightManager
- (void)showFreshlight
{
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([captureDevice hasTorch] && [captureDevice hasFlash])
{
if (captureDevice.torchMode == AVCaptureTorchModeOff)
{
[captureDevice lockForConfiguration:nil];
[captureDevice setTorchMode: AVCaptureTorchModeOn];
[captureDevice unlockForConfiguration];
}
else
{
[captureDevice lockForConfiguration:nil];
[captureDevice setTorchMode: AVCaptureTorchModeOff];
[captureDevice unlockForConfiguration];
}
}
else
{
[[[UIAlertView alloc] initWithTitle:@"温馨提醒"
message:@"抱歉,该设备没有闪光灯而无法使用闪光灯功能!"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil] show];
}
}