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 }
时间: 2024-10-01 06:25:00