Added front facing flash support

This commit is contained in:
Andrew Walz 2017-01-06 20:22:31 -07:00
parent 72b852fa4b
commit 29448b7c5c
5 changed files with 65 additions and 24 deletions

View file

@ -1,5 +1,10 @@
# CHANGELOG # CHANGELOG
## Version 1.3.0
- Added support for front facing flash
- Added support for front facing Retina Flash (on supported models)
- Added support for front facing torch mode for video capture
## Version 1.2.3 ## Version 1.2.3
- Added support for Swift Package Manager - Added support for Swift Package Manager

View file

@ -26,9 +26,10 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
super.viewDidLoad() super.viewDidLoad()
cameraDelegate = self cameraDelegate = self
kMaximumVideoDuration = 10.0 kMaximumVideoDuration = 10.0
tapToFocus = true }
pinchToZoom = true
lowLightBoost = true override var prefersStatusBarHidden: Bool {
return true
} }
override func viewDidAppear(_ animated: Bool) { override func viewDidAppear(_ animated: Bool) {

View file

@ -23,10 +23,11 @@ Configuring a Camera View Controller in AVFoundation can be tedious and time con
:movie_camera: | Video capture :movie_camera: | Video capture
:chart_with_upwards_trend: | Manual image quality settings :chart_with_upwards_trend: | Manual image quality settings
:tada: | Front and rear camera support :tada: | Front and rear camera support
:flashlight: | Torch/flash settings :flashlight: | Front and rear flash
:sunny: | Retina flash support
:mag_right: | Supports manual zoom :mag_right: | Supports manual zoom
:lock: | Supports manual focus :lock: | Supports manual focus
:last_quarter_moon_with_face: | low light setting :last_quarter_moon_with_face: | Low light setting
## Requirements ## Requirements
@ -141,8 +142,18 @@ and in your viewDidLoad, assign the cameraDelegate to self:
The flash(torch) can be enabled by changing the **flashEnabled** property: The flash(torch) can be enabled by changing the **flashEnabled** property:
flashEnabled = true flashEnabled = true
Flash is now supported for front and rear facing cameras.
The flash will only be enabled if the current camera is the rear camera. For photos, the camera will flash much like the stock iOS camera. For video, the torch(flash) will enable for the duration of the video capture. ### Rear Camera
For photos, the camera will flash much like the stock iOS camera. For video, the torch(flash) will enable for the duration of the video capture.
### Front Camera
For models that support [Retina Flash](https://developer.apple.com/library/content/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/Cameras/Cameras.html#//apple_ref/doc/uid/TP40013599-CH107-SW7), the front camera will use the default flash for image capture. If Retina Flash is not supported, a faux Retina Flash is used similar to Snapchat.
For front facing videos, a white, 85% opaque view will be placed over the video feed for the duration of the video capture.
## Switching Camera ## Switching Camera

View file

@ -126,10 +126,6 @@ open class SwiftyCamViewController: UIViewController {
// MARK: Public Get-only Variable Declarations // MARK: Public Get-only Variable Declarations
/// Returns true if the torch (flash) is currently enabled
private(set) public var isCameraFlashOn = false
/// Returns true if video is currently being recorded /// Returns true if video is currently being recorded
private(set) public var isVideRecording = false private(set) public var isVideRecording = false
@ -162,6 +158,10 @@ open class SwiftyCamViewController: UIViewController {
fileprivate var beginZoomScale = CGFloat(1.0) fileprivate var beginZoomScale = CGFloat(1.0)
/// Returns true if the torch (flash) is currently enabled
fileprivate var isCameraTorchOn = false
/// Variable to store result of capture session setup /// Variable to store result of capture session setup
fileprivate var setupResult = SessionSetupResult.success fileprivate var setupResult = SessionSetupResult.success
@ -190,8 +190,13 @@ open class SwiftyCamViewController: UIViewController {
fileprivate var previewLayer : PreviewView! fileprivate var previewLayer : PreviewView!
/// UIView for front facing flash
fileprivate var flashView : UIView?
/// Disable view autorotation for forced portrait recorindg /// Disable view autorotation for forced portrait recorindg
override open var shouldAutorotate: Bool { override open var shouldAutorotate: Bool {
return false return false
} }
@ -306,20 +311,20 @@ open class SwiftyCamViewController: UIViewController {
capturePhotoAsyncronously(completionHandler: { (_) in }) capturePhotoAsyncronously(completionHandler: { (_) in })
} else if device.hasFlash == false && flashEnabled == true && currentCamera == .front { } else if device.hasFlash == false && flashEnabled == true && currentCamera == .front {
let flashView = UIView(frame: view.frame) flashView = UIView(frame: view.frame)
flashView.alpha = 0.0 flashView?.alpha = 0.0
flashView.backgroundColor = UIColor.white flashView?.backgroundColor = UIColor.white
view.addSubview(flashView) previewLayer.addSubview(flashView!)
UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: { UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: {
flashView.alpha = 1.0 self.flashView?.alpha = 1.0
}, completion: { (_) in }, completion: { (_) in
self.capturePhotoAsyncronously(completionHandler: { (success) in self.capturePhotoAsyncronously(completionHandler: { (success) in
UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: { UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: {
flashView.alpha = 0.0 self.flashView?.alpha = 0.0
}, completion: { (_) in }, completion: { (_) in
flashView.removeFromSuperview() self.flashView?.removeFromSuperview()
}) })
}) })
}) })
@ -348,6 +353,17 @@ open class SwiftyCamViewController: UIViewController {
enableFlash() enableFlash()
} }
if currentCamera == .front && flashEnabled == true {
flashView = UIView(frame: view.frame)
flashView?.backgroundColor = UIColor.white
flashView?.alpha = 0.0
previewLayer.addSubview(flashView!)
UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: {
self.flashView?.alpha = 0.85
}, completion: nil)
}
let videoPreviewLayerOrientation = previewLayer!.videoPreviewLayer.connection.videoOrientation let videoPreviewLayerOrientation = previewLayer!.videoPreviewLayer.connection.videoOrientation
sessionQueue.async { [unowned self] in sessionQueue.async { [unowned self] in
@ -394,6 +410,14 @@ open class SwiftyCamViewController: UIViewController {
self.isVideRecording = false self.isVideRecording = false
movieFileOutput!.stopRecording() movieFileOutput!.stopRecording()
disableFlash() disableFlash()
if currentCamera == .front && flashEnabled == true && flashView != nil {
UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveEaseInOut, animations: {
self.flashView?.alpha = 0.0
}, completion: { (_) in
self.flashView?.removeFromSuperview()
})
}
self.cameraDelegate?.SwiftyCamDidFinishRecordingVideo() self.cameraDelegate?.SwiftyCamDidFinishRecordingVideo()
} }
} }
@ -438,7 +462,7 @@ open class SwiftyCamViewController: UIViewController {
self.session.startRunning() self.session.startRunning()
} }
// If flash is enabled, disable it as flash is not supported or needed for front facing camera // If flash is enabled, disable it as the torch is needed for front facing camera
disableFlash() disableFlash()
} }
@ -783,7 +807,7 @@ open class SwiftyCamViewController: UIViewController {
/// Enable flash /// Enable flash
fileprivate func enableFlash() { fileprivate func enableFlash() {
if self.isCameraFlashOn == false { if self.isCameraTorchOn == false {
toggleFlash() toggleFlash()
} }
} }
@ -791,7 +815,7 @@ open class SwiftyCamViewController: UIViewController {
/// Disable flash /// Disable flash
fileprivate func disableFlash() { fileprivate func disableFlash() {
if self.isCameraFlashOn == true { if self.isCameraTorchOn == true {
toggleFlash() toggleFlash()
} }
} }
@ -811,11 +835,11 @@ open class SwiftyCamViewController: UIViewController {
try device?.lockForConfiguration() try device?.lockForConfiguration()
if (device?.torchMode == AVCaptureTorchMode.on) { if (device?.torchMode == AVCaptureTorchMode.on) {
device?.torchMode = AVCaptureTorchMode.off device?.torchMode = AVCaptureTorchMode.off
self.isCameraFlashOn = false self.isCameraTorchOn = false
} else { } else {
do { do {
try device?.setTorchModeOnWithLevel(1.0) try device?.setTorchModeOnWithLevel(1.0)
self.isCameraFlashOn = true self.isCameraTorchOn = true
} catch { } catch {
print("[SwiftyCam]: \(error)") print("[SwiftyCam]: \(error)")
} }

View file

@ -8,8 +8,8 @@
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = 'SwiftyCam' s.name = 'SwiftyCam'
s.version = '1.2.3' s.version = '1.3.0'
s.summary = 'A Simple, Snapchat-style camera Framework written in Swift' s.summary = 'A Simple, Snapchat inspired camera Framework written in Swift'
s.ios.deployment_target = '8.0' s.ios.deployment_target = '8.0'