New flash settings working for rear camera

This commit is contained in:
Andrew Walz 2017-01-06 11:55:34 -07:00
parent bf4bd9ce97
commit 1abe380628
3 changed files with 60 additions and 32 deletions

View file

@ -71,7 +71,7 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
}
@IBAction func toggleFlashAction(_ sender: Any) {
toggleFlash()
flashEnabled = !flashEnabled
}
}

View file

@ -75,6 +75,13 @@ open class SwiftyCamViewController: UIViewController {
public var videoQuality : VideoQuality = .high
// Sets whether camera flash will be used to take photo
// For photos, camera flash will be enabled when photo is taken
// For videos, camera flash will be enabled for the duration of the video
// Not currently supported for front facing camera
public var flashEnabled = false
// Sets whether Pinch to Zoom is supported for the capture session
// Pinch to zoom not supported on front facing camera
@ -131,7 +138,7 @@ open class SwiftyCamViewController: UIViewController {
fileprivate var beginZoomScale = CGFloat(1.0)
// Variable for storing result of Capture Session setup
// Variable to store result of capture session setup
fileprivate var setupResult = SessionSetupResult.success
@ -241,7 +248,7 @@ open class SwiftyCamViewController: UIViewController {
self.isSessionRunning = false
}
//Disble flash if it is currently enables
//Disble flash if it is currently enabled
disableFlash()
}
@ -250,6 +257,21 @@ open class SwiftyCamViewController: UIViewController {
// Capture photo from session
public func takePhoto() {
guard videoDevice != nil else {
return
}
if videoDevice!.hasFlash && currentCamera == .rear && flashEnabled == true /* TODO: Add Support for Retina Flash and add front flash */ {
do {
try videoDevice!.lockForConfiguration()
videoDevice?.flashMode = .on
videoDevice!.unlockForConfiguration()
} catch {
print("[SwiftyCam]: \(error)")
}
}
if let videoConnection = photoFileOutput?.connection(withMediaType: AVMediaTypeVideo) {
videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait
photoFileOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(sampleBuffer, error) in
@ -271,6 +293,10 @@ open class SwiftyCamViewController: UIViewController {
return
}
if currentCamera == .rear && flashEnabled == true {
enableFlash()
}
let videoPreviewLayerOrientation = previewLayer!.videoPreviewLayer.connection.videoOrientation
sessionQueue.async { [unowned self] in
@ -308,6 +334,7 @@ open class SwiftyCamViewController: UIViewController {
if self.movieFileOutput?.isRecording == true {
self.isVideRecording = false
movieFileOutput!.stopRecording()
disableFlash()
self.cameraDelegate?.SwiftyCamDidFinishRecordingVideo()
}
}
@ -349,35 +376,6 @@ open class SwiftyCamViewController: UIViewController {
disableFlash()
}
public func toggleFlash() {
guard self.currentCamera == .rear else {
// Flash is not supported for front facing camera
return
}
let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
// Check if device has a flash
if (device?.hasTorch)! {
do {
try device?.lockForConfiguration()
if (device?.torchMode == AVCaptureTorchMode.on) {
device?.torchMode = AVCaptureTorchMode.off
self.isCameraFlashOn = false
} else {
do {
try device?.setTorchModeOnWithLevel(1.0)
self.isCameraFlashOn = true
} catch {
print("[SwiftyCam]: \(error)")
}
}
device?.unlockForConfiguration()
} catch {
print("[SwiftyCam]: \(error)")
}
}
}
// Override Touches Began
// Used for Tap to Focus
@ -676,6 +674,35 @@ open class SwiftyCamViewController: UIViewController {
toggleFlash()
}
}
fileprivate func toggleFlash() {
guard self.currentCamera == .rear else {
// Flash is not supported for front facing camera
return
}
let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
// Check if device has a flash
if (device?.hasTorch)! {
do {
try device?.lockForConfiguration()
if (device?.torchMode == AVCaptureTorchMode.on) {
device?.torchMode = AVCaptureTorchMode.off
self.isCameraFlashOn = false
} else {
do {
try device?.setTorchModeOnWithLevel(1.0)
self.isCameraFlashOn = true
} catch {
print("[SwiftyCam]: \(error)")
}
}
device?.unlockForConfiguration()
} catch {
print("[SwiftyCam]: \(error)")
}
}
}
}
extension SwiftyCamViewController : SwiftyCamButtonDelegate {

View file

@ -61,6 +61,7 @@ public protocol SwiftyCamViewControllerDelegate {
}
public extension SwiftyCamViewControllerDelegate {
func SwiftyCamDidTakePhoto(_ photo:UIImage) {
// Optional
}