打开闪光灯的代码
Posted 活最好的自己
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了打开闪光灯的代码相关的知识,希望对你有一定的参考价值。
1.比较常用,也比较简单,因此粘贴到这里
1 - (void)onFlash:(UIButton *)sender{ 2 3 AVCaptureDevice *avDevice = self.device; 4 if (![avDevice hasFlash] || ![avDevice hasTorch]) { 5 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"打开摄像头失败!请检测您的摄像头!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil]; 6 [alert show]; 7 return; 8 } 9 10 sender.selected = !sender.selected; 11 [self openFlashLight:sender]; 12 } 13 14 - (void)openFlashLight:(UIButton *)sender 15 { 16 AVCaptureSession *avSession = self.captureSession; 17 AVCaptureDevice *avDevice = self.device; 18 if (sender.selected) { 19 if (avDevice.torchMode == AVCaptureTorchModeOff) { 20 [avSession beginConfiguration]; 21 [avDevice lockForConfiguration:nil]; 22 [avDevice setTorchMode:AVCaptureTorchModeOn]; 23 [avDevice setFlashMode:AVCaptureFlashModeOn]; 24 [avDevice unlockForConfiguration]; 25 } 26 } 27 else 28 { 29 if (avDevice.torchMode == AVCaptureTorchModeOn) { 30 [avSession beginConfiguration]; 31 [avDevice lockForConfiguration:nil]; 32 [avDevice setTorchMode:AVCaptureTorchModeOff]; 33 [avDevice setFlashMode:AVCaptureFlashModeOff]; 34 [avDevice unlockForConfiguration]; 35 } 36 } 37 }
以上是关于打开闪光灯的代码的主要内容,如果未能解决你的问题,请参考以下文章