iOS 开发 用代码打开手电筒

最近项目中有扫描二维码的功能,我想添加一个打开手电筒的操作,实现过程如下:

 在项目中导入 AVFoundation.framework 依赖库,在实现类中导入头文件

#import  

实现代码如下:


// 开关按钮

- (void)RightButton

{

//    AVCaptureDevice *device = [UIDevice currentDevice];

self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

//修改前必须先锁定

[self.device lockForConfiguration:nil];

//必须判定是否有闪光灯,否则如果没有闪光灯会崩溃

if ([self.device hasFlash]) {

if (self.device.flashMode == AVCaptureFlashModeOff) {

self.device.flashMode = AVCaptureFlashModeOn;

self.device.torchMode = AVCaptureTorchModeOn;

} else if (self.device.flashMode == AVCaptureFlashModeOn) {

self.device.flashMode = AVCaptureFlashModeOff;

self.device.torchMode = AVCaptureTorchModeOff;

}

}

[self.device unlockForConfiguration];

}

你可能感兴趣的:(iOS 开发 用代码打开手电筒)