如何打开/closuresiPhone相机闪光灯2?

我正在寻找如何打开/closuresiPhone的相机闪光灯,我发现这一点:

@IBAction func didTouchFlashButton(sender: AnyObject) { let avDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) // check if the device has torch if avDevice.hasTorch { // lock your device for configuration avDevice.lockForConfiguration(nil) // check if your torchMode is on or off. If on turns it off otherwise turns it on if avDevice.torchActive { avDevice.torchMode = AVCaptureTorchMode.Off } else { // sets the torch intensity to 100% avDevice.setTorchModeOnWithLevel(1.0, error: nil) } // unlock your device avDevice.unlockForConfiguration() } } 

我有两个问题,一个在线:

 avDevice.lockForConfiguration(nil) 

另一个就行了:

 avDevice.setTorchModeOnWithLevel(1.0, error:nil) 

他们都与exception处理有关,但我不知道如何解决它们。

 @IBAction func didTouchFlashButton(sender: UIButton) { let avDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) // check if the device has torch if avDevice.hasTorch { // lock your device for configuration do { let abv = try avDevice.lockForConfiguration() } catch { print("aaaa") } // check if your torchMode is on or off. If on turns it off otherwise turns it on if avDevice.torchActive { avDevice.torchMode = AVCaptureTorchMode.Off } else { // sets the torch intensity to 100% do { let abv = try avDevice.setTorchModeOnWithLevel(1.0) } catch { print("bbb") } // avDevice.setTorchModeOnWithLevel(1.0, error: nil) } // unlock your device avDevice.unlockForConfiguration() } } 
 import AVFoundation var videoDeviceInput: AVCaptureDeviceInput? var movieFileOutput: AVCaptureMovieFileOutput? var stillImageOutput: AVCaptureStillImageOutput? 

向ViewController添加一个类的方法。

 class func setFlashMode(flashMode: AVCaptureFlashMode, device: AVCaptureDevice){ if device.hasFlash && device.isFlashModeSupported(flashMode) { var error: NSError? = nil do { try device.lockForConfiguration() device.flashMode = flashMode device.unlockForConfiguration() } catch let error1 as NSError { error = error1 print(error) } } } 

检查闪光灯状态。

 // Flash set to Auto/Off for Still Capture print("flashMode.rawValue : \(self.videoDeviceInput!.device.flashMode.rawValue)") if(self.videoDeviceInput!.device.flashMode.rawValue == 1) { CameraViewController.setFlashMode(AVCaptureFlashMode.On, device: self.videoDeviceInput!.device) } else if (self.videoDeviceInput!.device.flashMode.rawValue == 2) { CameraViewController.setFlashMode(AVCaptureFlashMode.Auto, device: self.videoDeviceInput!.device) } else { CameraViewController.setFlashMode(AVCaptureFlashMode.Off, device: self.videoDeviceInput!.device) } 

出于某种原因,“avDevice.torchActive”始终是错误的,即使在火炬打开的情况下,也无法closures,但我通过声明布尔值初始化设置为false来固定它,每当闪光灯打开时,布尔值被设置为真正。

 var on: Bool = false @IBAction func didTouchFlashButton(sender: UIButton) { let avDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) // check if the device has torch if avDevice.hasTorch { // lock your device for configuration do { let abv = try avDevice.lockForConfiguration() } catch { print("aaaa") } // check if your torchMode is on or off. If on turns it off otherwise turns it on if on == true { avDevice.torchMode = AVCaptureTorchMode.Off on = false } else { // sets the torch intensity to 100% do { let abv = try avDevice.setTorchModeOnWithLevel(1.0) on = true } catch { print("bbb") } // avDevice.setTorchModeOnWithLevel(1.0, error: nil) } // unlock your device avDevice.unlockForConfiguration() } 

}