我正在调用一个函数来尝试打开我设备的闪存:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
设置device.flashMode到AVCaptureDevice.FlashMode.auto带来了警告,"'flashMode’在iOS的10.0已被否决:使用AVCapturePhotoSettings.flashMode代替." 虽然它只是一个警告,但它在测试我的应用时不会启用闪存,因此我将该行更改为:
device.flashMode = AVCaptureDevice.FlashMode.auto
Run Code Online (Sandbox Code Playgroud)
所以我设置了这一行,就像它暗示:
AVCapturePhotoSettings.flashMode = AVCaptureDevice.FlashMode.auto
Run Code Online (Sandbox Code Playgroud)
我得到错误"实例成员'flashMode'不能用于类型'AVCapturePhotoSettings'"
所以我不知道如何使用Swift 4.0在Xcode版本9中设置闪存.我在Stack Overflow中找到的所有答案都是针对以前的版本.
小智 8
我一直面临着同样的问题.不幸的是,许多有用的方法在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
}
Run Code Online (Sandbox Code Playgroud)
如您所见,不需要lockConfiguration.
CurrentFlashMode是枚举,它是为了保持清晰而创建的:
@IBAction func captureButtonPressed(_ sender: UIButton) {
let settings = getSettings(camera: camera, flashMode: flashMode)
photoOutput.capturePhoto(with: settings, delegate: self)
}
Run Code Online (Sandbox Code Playgroud)
然后在拍摄照片时使用它:
/// 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
}
Run Code Online (Sandbox Code Playgroud)
希望它会有所帮助.
对于目标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];
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4525 次 |
最近记录: |