diff --git a/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/AppIcon.appiconset/Contents.json b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/AppIcon.appiconset/Contents.json index b8236c6..19882d5 100644 --- a/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/AppIcon.appiconset/Contents.json +++ b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -39,6 +39,11 @@ "idiom" : "iphone", "size" : "60x60", "scale" : "3x" + }, + { + "idiom" : "ios-marketing", + "size" : "1024x1024", + "scale" : "1x" } ], "info" : { diff --git a/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/flashauto.imageset/Contents.json b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/flashauto.imageset/Contents.json new file mode 100644 index 0000000..6cd836f --- /dev/null +++ b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/flashauto.imageset/Contents.json @@ -0,0 +1,22 @@ +{ + "images" : [ + { + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "filename" : "flashauto@2x.png", + "scale" : "2x" + }, + { + "idiom" : "universal", + "filename" : "flashauto@3x.png", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/flashauto.imageset/flashauto@2x.png b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/flashauto.imageset/flashauto@2x.png new file mode 100644 index 0000000..8d15610 Binary files /dev/null and b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/flashauto.imageset/flashauto@2x.png differ diff --git a/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/flashauto.imageset/flashauto@3x.png b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/flashauto.imageset/flashauto@3x.png new file mode 100644 index 0000000..cc7666e Binary files /dev/null and b/DemoSwiftyCam/DemoSwiftyCam/Assets.xcassets/flashauto.imageset/flashauto@3x.png differ diff --git a/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift b/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift index 06a5d9b..00a2200 100644 --- a/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift +++ b/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift @@ -31,8 +31,8 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate { shouldUseDeviceOrientation = true allowAutoRotate = true audioEnabled = true - - // disable capture button until session starts + flashMode = .auto + flashButton.setImage(#imageLiteral(resourceName: "flashauto"), for: UIControlState()) captureButton.buttonEnabled = false } @@ -109,7 +109,7 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate { } @IBAction func toggleFlashTapped(_ sender: Any) { - flashEnabled = !flashEnabled + //flashEnabled = !flashEnabled toggleFlashAnimation() } } @@ -152,10 +152,16 @@ extension ViewController { } fileprivate func toggleFlashAnimation() { - if flashEnabled == true { - flashButton.setImage(#imageLiteral(resourceName: "flash"), for: UIControl.State()) - } else { - flashButton.setImage(#imageLiteral(resourceName: "flashOutline"), for: UIControl.State()) + //flashEnabled = !flashEnabled + if flashMode == .auto{ + flashMode = .on + flashButton.setImage(#imageLiteral(resourceName: "flash"), for: UIControlState()) + }else if flashMode == .on{ + flashMode = .off + flashButton.setImage(#imageLiteral(resourceName: "flashOutline"), for: UIControlState()) + }else if flashMode == .off{ + flashMode = .auto + flashButton.setImage(#imageLiteral(resourceName: "flashauto"), for: UIControlState()) } } } diff --git a/Source/SwiftyCamViewController.swift b/Source/SwiftyCamViewController.swift index d798e55..3df8da2 100644 --- a/Source/SwiftyCamViewController.swift +++ b/Source/SwiftyCamViewController.swift @@ -1,4 +1,4 @@ -/*Copyright (c) 2016, Andrew Walz. + /*Copyright (c) 2016, Andrew Walz. Redistribution and use in source and binary forms, with or without modification,are permitted provided that the following conditions are met: @@ -35,6 +35,18 @@ open class SwiftyCamViewController: UIViewController { /// Camera on the front of the device case front = "front" } + + public enum FlashMode{ + + //Flash mode is set to auto + case auto + + //Flash mode is set to on + case on + + //Flash mode is set to off + case off + } /// Enumeration for video quality of the capture session. Corresponds to a AVCaptureSessionPreset @@ -102,8 +114,11 @@ open class SwiftyCamViewController: UIViewController { public var videoQuality : VideoQuality = .high /// Sets whether flash is enabled for photo and video capture - + @available(*, deprecated, message: "use flashMode .on or .off") //use flashMode public var flashEnabled = false + + // Flash Mode + public var flashMode:FlashMode = .off /// Sets whether Pinch to Zoom is enabled for the capture session @@ -452,32 +467,32 @@ open class SwiftyCamViewController: UIViewController { return } - - if device.hasFlash == true && flashEnabled == true /* TODO: Add Support for Retina Flash and add front flash */ { - changeFlashSettings(device: device, mode: .on) + if device.hasFlash == true && flashMode != .off /* TODO: Add Support for Retina Flash and add front flash */ { + changeFlashSettings(device: device, mode: flashMode) capturePhotoAsyncronously(completionHandler: { (_) in }) - } else if device.hasFlash == false && flashEnabled == true && currentCamera == .front { - flashView = UIView(frame: view.frame) - flashView?.alpha = 0.0 - flashView?.backgroundColor = UIColor.white - previewLayer.addSubview(flashView!) - - UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: { - self.flashView?.alpha = 1.0 - - }, completion: { (_) in - self.capturePhotoAsyncronously(completionHandler: { (success) in - UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: { - self.flashView?.alpha = 0.0 - }, completion: { (_) in - self.flashView?.removeFromSuperview() - }) - }) - }) - } else { +// } else if device.hasFlash == false && flashMode == .on && currentCamera == .front { +// flashView = UIView(frame: view.frame) +// flashView?.alpha = 0.0 +// flashView?.backgroundColor = UIColor.white +// previewLayer.addSubview(flashView!) +// +// UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: { +// self.flashView?.alpha = 1.0 +// +// }, completion: { (_) in +// self.capturePhotoAsyncronously(completionHandler: { (success) in +// UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: { +// self.flashView?.alpha = 0.0 +// }, completion: { (_) in +// self.flashView?.removeFromSuperview() +// }) +// }) +// }) +// } else { + }else{ if device.isFlashActive == true { - changeFlashSettings(device: device, mode: .off) + changeFlashSettings(device: device, mode: flashMode) } capturePhotoAsyncronously(completionHandler: { (_) in }) } @@ -501,11 +516,11 @@ open class SwiftyCamViewController: UIViewController { return } - if currentCamera == .rear && flashEnabled == true { + if currentCamera == .rear && flashMode == .on { enableFlash() } - if currentCamera == .front && flashEnabled == true { + if currentCamera == .front && flashMode == .on { flashView = UIView(frame: view.frame) flashView?.backgroundColor = UIColor.white flashView?.alpha = 0.85 @@ -563,7 +578,7 @@ open class SwiftyCamViewController: UIViewController { movieFileOutput!.stopRecording() disableFlash() - if currentCamera == .front && flashEnabled == true && flashView != nil { + if currentCamera == .front && flashMode == .on && flashView != nil { UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: { self.flashView?.alpha = 0.0 }, completion: { (_) in @@ -917,9 +932,19 @@ open class SwiftyCamViewController: UIViewController { /// Enable or disable flash for photo - fileprivate func changeFlashSettings(device: AVCaptureDevice, mode: AVCaptureDevice.FlashMode) { +fileprivate func changeFlashSettings(device: AVCaptureDevice, mode: FlashMode) { do { try device.lockForConfiguration() + let mode:AVCaptureDevice.FlashMode + switch flashMode{ + case .on: + mode = .on + case .off: + mode = .off + case .auto: + mode = .auto + + } device.flashMode = mode device.unlockForConfiguration() } catch {