iOS 关闭和开启闪光灯

- (void)openLight:(UIButton *)sender{
    AVCaptureDevice *device = self.device;
    
    //修改前必须先锁定
    [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;
        }
        
    }
    [device unlockForConfiguration];
}

你可能感兴趣的:(iOS 关闭和开启闪光灯)