From 27194c99a9df4320c1fb4988eeb1f1eca745e87b Mon Sep 17 00:00:00 2001 From: Natalia Terlecka Date: Fri, 20 Mar 2015 14:36:35 +0000 Subject: [PATCH] Incrementing flash mode function. --- README.md | 6 ++++++ camera/CameraManager.swift | 42 ++++++++++++++++++++++++++----------- camera/ViewController.swift | 3 +-- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 31fc8ee..7d47978 100755 --- a/README.md +++ b/README.md @@ -52,6 +52,12 @@ CameraManager.sharedInstance.flashMode = .On CameraManager.sharedInstance.flashMode = .Auto ``` +To change flash mode to the next available one you can use this handy function which will also return current value for you to update the UI accordingly: +```swift +CameraManager.sharedInstance.changeFlashMode() +``` + + You can specify if you want to save the files to phone library: ```swift CameraManager.sharedInstance.writeFilesToPhoneLibrary = true diff --git a/camera/CameraManager.swift b/camera/CameraManager.swift index e5c41d6..e05702f 100644 --- a/camera/CameraManager.swift +++ b/camera/CameraManager.swift @@ -35,13 +35,20 @@ public enum CameraOutputQuality { /// Class for handling iDevices custom camera usage public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate { + // PRAGMA MARK - Public properties + + /// CameraManager singleton instance to use the camera. + public class var sharedInstance: CameraManager { + return _singletonSharedInstance + } + /// Capture session to customize camera settings. public var captureSession: AVCaptureSession? /// Property to determine if the manager should show the error for the user. If you want to show the errors yourself set this to false. If you want to add custom error UI set showErrorBlock property. Default value is false. public var showErrorsToUsers = false - /// Property to determine if the manager should show the camera permission popup immediatly when it's needed or you want to show it manually. Default value is true. + /// Property to determine if the manager should show the camera permission popup immediatly when it's needed or you want to show it manually. Default value is true. Be carful cause using the camera requires permission, if you set this value to false and don't ask manually you won't be able to use the camera. public var showAccessPermissionPopupAutomatically = true /// A block creating UI to present error message to the user. @@ -52,7 +59,7 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate { /// Property to determine if manager should write the resources to the phone library. Default value is true. public var writeFilesToPhoneLibrary = true - /// The Bool property to determin if current device has front camera. + /// The Bool property to determine if current device has front camera. public var hasFrontCamera: Bool = { let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) for device in devices { @@ -172,6 +179,8 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate { } } + // PRAGMA MARK - Private properties + private weak var embedingView: UIView? private var videoCompletition: ((videoURL: NSURL, error: NSError?) -> Void)? @@ -201,15 +210,8 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate { return NSURL(fileURLWithPath: tempPath!)! }() - /// CameraManager singleton instance to use the camera. - public class var sharedInstance: CameraManager { - return _singletonSharedInstance - } - - deinit { - self.stopAndRemoveCaptureSession() - self._stopFollowingDeviceOrientation() - } + + // PRAGMA MARK - CameraManager /** Inits a capture session and adds a preview layer to the given view. Preview layer bounds will automaticaly be set to match given view. Default session is initialized with still image output. @@ -390,7 +392,18 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate { { return self._checkIfCameraIsAvailable() } - + + /** + Change current flash mode to next value from available ones. + + :returns: Current flash mode: Off / On / Auto + */ + public func changeFlashMode() -> CameraFlashMode + { + self.flashMode = CameraFlashMode(rawValue: (self.flashMode.rawValue+1)%3)! + return self.flashMode + } + // PRAGMA MARK - AVCaptureFileOutputRecordingDelegate public func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) @@ -706,4 +719,9 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate { }) } } + + deinit { + self.stopAndRemoveCaptureSession() + self._stopFollowingDeviceOrientation() + } } diff --git a/camera/ViewController.swift b/camera/ViewController.swift index ec23916..afa58d0 100644 --- a/camera/ViewController.swift +++ b/camera/ViewController.swift @@ -72,8 +72,7 @@ class ViewController: UIViewController { @IBAction func changeFlashMode(sender: UIButton) { - self.cameraManager.flashMode = CameraFlashMode(rawValue: (self.cameraManager.flashMode.rawValue+1)%3)! - switch (self.cameraManager.flashMode) { + switch (self.cameraManager.changeFlashMode()) { case .Off: sender.setTitle("Flash Off", forState: UIControlState.Normal) case .On: