代码比较简单,就几行,不废话了,也不解释了,直接上代码吧。
1、导入头文件
1 #import <AVFoundation/AVFoundation.h>
2、点击按钮实现:
1 - (IBAction)torchOnTouchButton:(UIButton *)sender{ 2 Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice"); // 设备类 3 if (captureDeviceClass != nil) { 4 AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; // 输入 5 if ([device hasTorch]) { // 判断是否有闪光灯 6 [device lockForConfiguration:nil]; // 请求独占访问硬件设备 7 if (sender.tag == 0) { 8 sender.tag = 1; 9 [device setTorchMode:AVCaptureTorchModeOn]; // 手电筒开 10 }else{ 11 sender.tag = 0; 12 [device setTorchMode:AVCaptureTorchModeOff]; // 手电筒关 13 } 14 [device unlockForConfiguration]; // 请求解除独占访问硬件设备 15 } 16 } 17 }