在我的应用程序中,我可以选择手电筒灯。然而,只有iPhone 4和iPhone 4S有火炬灯。其他设备没有手电筒灯。如何找到当前的设备型号?请帮我。提前谢谢。
答案 0 :(得分:4)
您不应将设备型号用作是否存在功能的指示器。相反,请使用API来准确告诉您该功能是否存在。
在您的情况下,您希望使用AVCaptureDevice
的{{1}}属性:
-hasTorch
AV Foundation Programming Guide和AVCaptureDevice Class Reference
提供了更多信息答案 1 :(得分:3)
与上面的代码相比,您可以拥有更少的代码并使用更少的内存:
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
BOOL hasTorch = NO;
for (AVCaptureDevice *device in devices) {
if ([device hasTorch]) {
hasTorch = YES;
break;
}
}
hasTorch现在将包含正确的值
答案 2 :(得分:1)
此代码可让您的设备开启手电筒。但它也会检测手电筒是打开还是关闭,反之亦然。
- (void)torchOnOff: (BOOL) onOff {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch]) {
[device lockForConfiguration:nil];
if (device.torchMode == AVCaptureTorchModeOff) {
device.torchMode = AVCaptureTorchModeOn;
NSLog(@"Torch mode is on.");
} else {
device.torchMode = AVCaptureTorchModeOff;
NSLog(@"Torch mode is off.");
}
[device unlockForConfiguration];
}
}
答案 3 :(得分:0)
Swift 4
if let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) {
if (device.hasTorch) {
// Device has torch
} else {
// Device does not have torch
}
} else {
// Device does not support video type (and so, no torch)
}
答案 4 :(得分:0)
devicesWithMediaType:
现已弃用。
let discoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .back)
for device in discoverySession.devices {
if device.hasTorch {
return true
}
}
return false
答案 5 :(得分:0)
雨燕4
func deviceHasTorch() -> Bool {
return AVCaptureDevice.default(for: AVMediaType.video)?.hasTorch == true
}