我调用一个函数试图打开设备的闪光灯:
private func flashOn(device:AVCaptureDevice)
{
print("flashOn called");
do {
try device.lockForConfiguration()
// line below returns warning 'flashMode' was deprecated in iOS 10.0: Use AVCapturePhotoSettings.flashMode instead.
device.flashMode = AVCaptureDevice.FlashMode.auto
device.unlockForConfiguration()
} catch {
// handle error
print("flash on error");
}
}将device.flashMode设置为AVCaptureDevice.FlashMode.auto将显示警告"'flashMode‘在iOS 10.0中被废弃:使用AVCapturePhotoSettings.flashMode代替。“尽管这只是一个警告,但它在测试我的应用程序时不启用闪存,所以我将该行更改为:
device.flashMode = AVCaptureDevice.FlashMode.auto所以我设定了这条线,就像它所暗示的:
AVCapturePhotoSettings.flashMode = AVCaptureDevice.FlashMode.auto我得到错误“实例成员'flashMode‘不能在’AVCapturePhotoSettings‘类型上使用
因此,我不知道如何使用SWIFT4.0在Xcode版本9中设置闪存。我在Stack溢出中找到的所有答案都是针对早期版本的。
发布于 2017-11-30 13:52:33
我也面临着同样的问题。不幸的是,许多有用的方法在iOS10和11中被废弃了。
AVCapturePhotoSettings对象是唯一的,不能重用,因此每次使用此方法都需要获得新的设置:
/// the current flash mode
private var flashMode: AVCaptureDevice.FlashMode = .auto
/// Get settings
///
/// - Parameters:
/// - camera: the camera
/// - flashMode: the current flash mode
/// - Returns: AVCapturePhotoSettings
private func getSettings(camera: AVCaptureDevice, flashMode: AVCaptureDevice.FlashMode) -> AVCapturePhotoSettings {
let settings = AVCapturePhotoSettings()
if camera.hasFlash {
settings.flashMode = flashMode
}
return settings
}如您所见,不需要lockConfiguration。
然后在拍摄照片时使用它:
@IBAction func captureButtonPressed(_ sender: UIButton) {
let settings = getSettings(camera: camera, flashMode: flashMode)
photoOutput.capturePhoto(with: settings, delegate: self)
}希望能帮上忙。
发布于 2018-07-02 05:47:10
关于目标C:
- (IBAction)turnTorchOn: (UIButton *) sender {
sender.selected = !sender.selected;
BOOL on;
if (sender.selected) {
on = true;
} else {
on = false;
}
// check if flashlight available
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCapturePhotoSettings *photosettings = [AVCapturePhotoSettings photoSettings];
if ([device hasTorch] && [device hasFlash]) {
[device lockForConfiguration:nil];
if (on) {
[device setTorchMode:AVCaptureTorchModeOn];
photosettings.flashMode = AVCaptureFlashModeOn;
//torchIsOn = YES; //define as a variable/property if you need to know status
} else {
[device setTorchMode:AVCaptureTorchModeOff];
photosettings.flashMode = AVCaptureFlashModeOn;
//torchIsOn = NO;
}
[device unlockForConfiguration];
}
}
}https://stackoverflow.com/questions/47273601
复制相似问题