更新:我知道如何打开/关闭相机闪光灯。我想知道的是相机闪光灯是否已经点亮。
我想知道 iPhone 上的相机闪光灯是否点亮,但我还没有找到任何UIImagePickerController
允许我这样做的方法。我知道我们可以得到cameraFlashMode。但我想知道相机闪光灯是否已经点亮。
例如,如果模式是UIImagePickerControllerCameraFlashModeAuto
,在我控制之前相机闪光灯是否点亮,我想在做一些操作之前知道相机闪光灯的状态。
令人惊讶的是,这真的没有回答第一个回答的人甚至没有回答这个问题......
func torchButtonPressed() {
//
let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
do {
try device.lockForConfiguration()
} catch {
return
}
if device.torchMode == AVCaptureTorchMode.Off {
do {
device.torchMode = AVCaptureTorchMode.On
try device.setTorchModeOnWithLevel(AVCaptureMaxAvailableTorchLevel)
} catch {
print("no torch")
return
}
} else {
device.torchMode = AVCaptureTorchMode.Off
}
device.unlockForConfiguration()
}
您可以使用以下代码找到
#import <AVFoundation/AVFoundation.h>
- (void) turnTorchOn: (bool) on {
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
if (on) {
NSLog(@"Torch is ON");
} else {
NSLog(@"Torch is OFF");
}
[device unlockForConfiguration];
}
}
}
快乐编码...!!!
我遇到了和你一样的问题。
iOS 支持两种模式 - 闪光灯和手电筒。下面的代码检查每个是否可用,然后根据您调用的代码打开或关闭它们。它还检查灯是否已经打开/关闭。
闪光灯开 -
-(void)flashOn {
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
if ([device hasFlash]) {
if ([device flashMode] == AVCaptureFlashModeOff) {
[device setFlashMode:AVCaptureFlashModeOn];
}
}
if ([device hasTorch]) {
if ([device torchMode] == AVCaptureTorchModeOff) {
[device setTorchMode:AVCaptureTorchModeOn];
}
}
[device unlockForConfiguration];
}
}
闪光灯关闭-
-(void)flashOff {
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[device lockForConfiguration:nil];
if ([device hasFlash]) {
if ([device flashMode] == AVCaptureFlashModeOn) {
[device setFlashMode:AVCaptureFlashModeOff];
}
}
if ([device hasTorch]) {
if ([device torchMode] == AVCaptureTorchModeOn) {
[device setTorchMode:AVCaptureTorchModeOff];
}
}
[device unlockForConfiguration];
}
}