Compare commits

..

No commits in common. "master" and "2.2.4" have entirely different histories.

10 changed files with 405 additions and 462 deletions

View File

@ -2,29 +2,8 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
## [3.0.0](https://github.com/imaginary-cloud/CameraManager/tree/3.0.0) - 2016-09-16
### Changed
- Syntax update for Swift 3.0.
## [2.2.4](https://github.com/imaginary-cloud/CameraManager/tree/2.2.4) - 2016-07-06
### Added
- Add error checking.
### Changed
- Fixes completion typos and suggests renamed functions.
## [2.2.3](https://github.com/imaginary-cloud/CameraManager/tree/2.2.3) - 2016-05-12
### Changed
- Fixed zoom in StillImage Mode.
- Minor refactoring
## [2.2.2](https://github.com/imaginary-cloud/CameraManager/tree/2.2.2) - 2016-03-07
### Added
- `CHANGELOG.md` file.

View File

@ -1,12 +1,12 @@
Pod::Spec.new do |s|
s.name = "CameraManager"
s.version = "3.0.0"
s.version = "2.2.4"
s.summary = "This is a simple Swift class to provide all the configurations you need to create custom camera view in your app. Just drag, drop and use."
s.requires_arc = true
s.homepage = "https://github.com/imaginary-cloud/CameraManager"
s.license = 'MIT'
s.author = { "torrao" => "rtorrao@imaginarycloud.com" }
s.source = { :git => "https://github.com/imaginary-cloud/CameraManager.git", :tag => "3.0.0" }
s.source = { :git => "https://github.com/imaginary-cloud/CameraManager.git", :tag => "2.2.4" }
s.social_media_url = 'http://www.imaginarycloud.com/'
s.platform = :ios, '8.0'
s.source_files = 'camera/CameraManager.swift'

View File

@ -14,14 +14,6 @@ The easiest way to install the CameraManager is with: [CocoaPods](http://cocoapo
### Podfile
If you want Swift 3.0 syntax use:
```ruby
use_frameworks!
pod 'CameraManager', '~> 3.0'
```
If you want Swift 2.0 syntax use:
```ruby
@ -60,12 +52,6 @@ let package = Package(
Add the following line to your Cartfile:
If you want Swift 3.0 syntax use:
```
github "imaginary-cloud/CameraManager" >= 3.0
```
If you want Swift 2.0 syntax use:
```

View File

@ -201,17 +201,14 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0800;
LastUpgradeCheck = 0700;
ORGANIZATIONNAME = imaginaryCloud;
TargetAttributes = {
454C1F4019E82E2500C81915 = {
CreatedOnToolsVersion = 6.0.1;
DevelopmentTeam = QM7HJTY23M;
LastSwiftMigration = 0800;
};
D71DE8801AD677A7001E62F1 = {
CreatedOnToolsVersion = 6.3;
LastSwiftMigration = 0800;
};
};
};
@ -316,10 +313,8 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_ENTITLEMENTS[sdk=iphoneos*]" = "";
@ -329,7 +324,6 @@
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
@ -364,10 +358,8 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
@ -375,7 +367,6 @@
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
@ -385,7 +376,6 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
@ -395,13 +385,11 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = camera/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = cameraDemo;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
@ -409,20 +397,17 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = camera/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = cameraDemo;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Release;
};
D71DE89B1AD677A8001E62F1 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
CURRENT_PROJECT_VERSION = 1;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEFINES_MODULE = YES;
@ -441,7 +426,6 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.imaginarycloud.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
@ -450,7 +434,6 @@
D71DE89C1AD677A8001E62F1 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 1;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
@ -466,7 +449,6 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.imaginarycloud.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0800"
LastUpgradeVersion = "0700"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
@ -23,21 +23,21 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
@ -55,10 +55,10 @@
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference

View File

@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

View File

@ -8,57 +8,58 @@
import UIKit
import AVFoundation
import Photos
import AssetsLibrary
public enum CameraState {
case ready, accessDenied, noDeviceFound, notDetermined
case Ready, AccessDenied, NoDeviceFound, NotDetermined
}
public enum CameraDevice {
case front, back
case Front, Back
}
public enum CameraFlashMode: Int {
case off, on, auto
case Off, On, Auto
}
public enum CameraOutputMode {
case stillImage, videoWithMic, videoOnly
case StillImage, VideoWithMic, VideoOnly
}
public enum CameraOutputQuality: Int {
case low, medium, high
case Low, Medium, High
}
/// Class for handling iDevices custom camera usage
open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGestureRecognizerDelegate {
public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGestureRecognizerDelegate {
// MARK: - Public properties
/// Capture session to customize camera settings.
open var captureSession: AVCaptureSession?
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.
open var showErrorsToUsers = 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. 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.
open var showAccessPermissionPopupAutomatically = true
public var showAccessPermissionPopupAutomatically = true
/// A block creating UI to present error message to the user. This can be customised to be presented on the Window root view controller, or to pass in the viewController which will present the UIAlertController, for example.
open var showErrorBlock:(_ erTitle: String, _ erMessage: String) -> Void = { (erTitle: String, erMessage: String) -> Void in
public var showErrorBlock:(erTitle: String, erMessage: String) -> Void = { (erTitle: String, erMessage: String) -> Void in
// var alertController = UIAlertController(title: erTitle, message: erMessage, preferredStyle: .Alert)
// alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (alertAction) -> Void in }))
//
// if let topController = UIApplication.sharedApplication().keyWindow?.rootViewController {
// topController.presentViewController(alertController, animated: true, completion:nil)
// }
// var alertController = UIAlertController(title: erTitle, message: erMessage, preferredStyle: .Alert)
// alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (alertAction) -> Void in }))
//
// if let topController = UIApplication.sharedApplication().keyWindow?.rootViewController {
// topController.presentViewController(alertController, animated: true, completion:nil)
// }
}
/// Property to determine if manager should write the resources to the phone library. Default value is true.
open var writeFilesToPhoneLibrary = true
public var writeFilesToPhoneLibrary = true
/// Property to determine if manager should follow device orientation. Default value is true.
open var shouldRespondToOrientationChanges = true {
public var shouldRespondToOrientationChanges = true {
didSet {
if shouldRespondToOrientationChanges {
_startFollowingDeviceOrientation()
@ -69,18 +70,18 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
/// The Bool property to determine if the camera is ready to use.
open var cameraIsReady: Bool {
public var cameraIsReady: Bool {
get {
return cameraIsSetup
}
}
/// The Bool property to determine if current device has front camera.
open var hasFrontCamera: Bool = {
let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo)
for device in devices! {
public var hasFrontCamera: Bool = {
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
for device in devices {
let captureDevice = device as! AVCaptureDevice
if (captureDevice.position == .front) {
if (captureDevice.position == .Front) {
return true
}
}
@ -88,19 +89,19 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}()
/// The Bool property to determine if current device has flash.
open var hasFlash: Bool = {
let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo)
for device in devices! {
public var hasFlash: Bool = {
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
for device in devices {
let captureDevice = device as! AVCaptureDevice
if (captureDevice.position == .back) {
if (captureDevice.position == .Back) {
return captureDevice.hasFlash
}
}
return false
}()
}()
/// Property to change camera device between front and back.
open var cameraDevice = CameraDevice.back {
public var cameraDevice = CameraDevice.Back {
didSet {
if cameraIsSetup {
if cameraDevice != oldValue {
@ -113,7 +114,7 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
/// Property to change camera flash mode.
open var flashMode = CameraFlashMode.off {
public var flashMode = CameraFlashMode.Off {
didSet {
if cameraIsSetup {
if flashMode != oldValue {
@ -124,7 +125,7 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
/// Property to change camera output quality.
open var cameraOutputQuality = CameraOutputQuality.high {
public var cameraOutputQuality = CameraOutputQuality.High {
didSet {
if cameraIsSetup {
if cameraOutputQuality != oldValue {
@ -135,7 +136,7 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
/// Property to change camera output.
open var cameraOutputMode = CameraOutputMode.stillImage {
public var cameraOutputMode = CameraOutputMode.StillImage {
didSet {
if cameraIsSetup {
if cameraOutputMode != oldValue {
@ -148,75 +149,80 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
/// Property to check video recording duration when in progress
open var recordedDuration : CMTime { return movieOutput?.recordedDuration ?? kCMTimeZero }
public var recordedDuration : CMTime { return movieOutput?.recordedDuration ?? kCMTimeZero }
/// Property to check video recording file size when in progress
open var recordedFileSize : Int64 { return movieOutput?.recordedFileSize ?? 0 }
public var recordedFileSize : Int64 { return movieOutput?.recordedFileSize ?? 0 }
// MARK: - Private properties
fileprivate weak var embeddingView: UIView?
fileprivate var videoCompletion: ((_ videoURL: URL?, _ error: NSError?) -> Void)?
private weak var embeddingView: UIView?
private var videoCompletion: ((videoURL: NSURL?, error: NSError?) -> Void)?
fileprivate var sessionQueue: DispatchQueue = DispatchQueue(label: "CameraSessionQueue", attributes: [])
private var sessionQueue: dispatch_queue_t = dispatch_queue_create("CameraSessionQueue", DISPATCH_QUEUE_SERIAL)
fileprivate lazy var frontCameraDevice: AVCaptureDevice? = {
let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) as! [AVCaptureDevice]
return devices.filter{$0.position == .front}.first
private lazy var frontCameraDevice: AVCaptureDevice? = {
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! [AVCaptureDevice]
return devices.filter{$0.position == .Front}.first
}()
fileprivate lazy var backCameraDevice: AVCaptureDevice? = {
let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) as! [AVCaptureDevice]
return devices.filter{$0.position == .back}.first
private lazy var backCameraDevice: AVCaptureDevice? = {
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! [AVCaptureDevice]
return devices.filter{$0.position == .Back}.first
}()
fileprivate lazy var mic: AVCaptureDevice? = {
return AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio)
private lazy var mic: AVCaptureDevice? = {
return AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)
}()
fileprivate var stillImageOutput: AVCaptureStillImageOutput?
fileprivate var movieOutput: AVCaptureMovieFileOutput?
fileprivate var previewLayer: AVCaptureVideoPreviewLayer?
fileprivate var library: PHPhotoLibrary?
private var stillImageOutput: AVCaptureStillImageOutput?
private var movieOutput: AVCaptureMovieFileOutput?
private var previewLayer: AVCaptureVideoPreviewLayer?
private var library: ALAssetsLibrary?
fileprivate var cameraIsSetup = false
fileprivate var cameraIsObservingDeviceOrientation = false
private var cameraIsSetup = false
private var cameraIsObservingDeviceOrientation = false
fileprivate var zoomScale = CGFloat(1.0)
fileprivate var beginZoomScale = CGFloat(1.0)
fileprivate var maxZoomScale = CGFloat(1.0)
private var zoomScale = CGFloat(1.0)
private var beginZoomScale = CGFloat(1.0)
private var maxZoomScale = CGFloat(1.0)
fileprivate var tempFilePath: URL = {
let tempPath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("tempMovie").appendingPathExtension("mp4").absoluteString
if FileManager.default.fileExists(atPath: tempPath) {
private var tempFilePath: NSURL = {
let tempPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("tempMovie").URLByAppendingPathExtension("mp4").absoluteString
if NSFileManager.defaultManager().fileExistsAtPath(tempPath) {
do {
try FileManager.default.removeItem(atPath: tempPath)
try NSFileManager.defaultManager().removeItemAtPath(tempPath)
} catch { }
}
return URL(string: tempPath)!
return NSURL(string: tempPath)!
}()
// 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.
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.
:param: view The view you want to add the preview layer to
:param: cameraOutputMode The mode you want capturesession to run image / video / video and microphone
:param: completion Optional completion block
:param: view The view you want to add the preview layer to
:param: cameraOutputMode The mode you want capturesession to run image / video / video and microphone
:param: completion Optional completion block
:returns: Current state of the camera: Ready / AccessDenied / NoDeviceFound / NotDetermined.
*/
open func addPreviewLayerToView(_ view: UIView) -> CameraState {
:returns: Current state of the camera: Ready / AccessDenied / NoDeviceFound / NotDetermined.
*/
public func addPreviewLayerToView(view: UIView) -> CameraState {
return addPreviewLayerToView(view, newCameraOutputMode: cameraOutputMode)
}
open func addPreviewLayerToView(_ view: UIView, newCameraOutputMode: CameraOutputMode) -> CameraState {
public func addPreviewLayerToView(view: UIView, newCameraOutputMode: CameraOutputMode) -> CameraState {
return addLayerPreviewToView(view, newCameraOutputMode: newCameraOutputMode, completion: nil)
}
open func addLayerPreviewToView(_ view: UIView, newCameraOutputMode: CameraOutputMode, completion: ((Void) -> Void)?) -> CameraState {
@available(*, unavailable, renamed="addLayerPreviewToView")
public func addPreviewLayerToView(view: UIView, newCameraOutputMode: CameraOutputMode, completition: (Void -> Void)?) -> CameraState {
return addLayerPreviewToView(view, newCameraOutputMode: newCameraOutputMode, completion: completition)
}
public func addLayerPreviewToView(view: UIView, newCameraOutputMode: CameraOutputMode, completion: (Void -> Void)?) -> CameraState {
if _canLoadCamera() {
if let _ = embeddingView {
if let validPreviewLayer = previewLayer {
@ -242,21 +248,24 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
return _checkIfCameraIsAvailable()
}
/**
Asks the user for camera permissions. Only works if the permissions are not yet determined. Note that it'll also automaticaly ask about the microphone permissions if you selected VideoWithMic output.
@available(*, unavailable, renamed="askUserForCameraPermission")
public func askUserForCameraPermissions(completition: Bool -> Void) {}
:param: completion Completion block with the result of permission request
*/
open func askUserForCameraPermission(_ completion: @escaping (Bool) -> Void) {
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (alowedAccess) -> Void in
if self.cameraOutputMode == .videoWithMic {
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeAudio, completionHandler: { (alowedAccess) -> Void in
DispatchQueue.main.sync(execute: { () -> Void in
/**
Asks the user for camera permissions. Only works if the permissions are not yet determined. Note that it'll also automaticaly ask about the microphone permissions if you selected VideoWithMic output.
:param: completion Completion block with the result of permission request
*/
public func askUserForCameraPermission(completion: Bool -> Void) {
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (alowedAccess) -> Void in
if self.cameraOutputMode == .VideoWithMic {
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeAudio, completionHandler: { (alowedAccess) -> Void in
dispatch_sync(dispatch_get_main_queue(), { () -> Void in
completion(alowedAccess)
})
})
} else {
DispatchQueue.main.sync(execute: { () -> Void in
dispatch_sync(dispatch_get_main_queue(), { () -> Void in
completion(alowedAccess)
})
@ -265,19 +274,19 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
/**
Stops running capture session but all setup devices, inputs and outputs stay for further reuse.
*/
open func stopCaptureSession() {
Stops running capture session but all setup devices, inputs and outputs stay for further reuse.
*/
public func stopCaptureSession() {
captureSession?.stopRunning()
_stopFollowingDeviceOrientation()
}
/**
Resumes capture session.
*/
open func resumeCaptureSession() {
Resumes capture session.
*/
public func resumeCaptureSession() {
if let validCaptureSession = captureSession {
if !validCaptureSession.isRunning && cameraIsSetup {
if !validCaptureSession.running && cameraIsSetup {
validCaptureSession.startRunning()
_startFollowingDeviceOrientation()
}
@ -297,11 +306,11 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
/**
Stops running capture session and removes all setup devices, inputs and outputs.
*/
open func stopAndRemoveCaptureSession() {
Stops running capture session and removes all setup devices, inputs and outputs.
*/
public func stopAndRemoveCaptureSession() {
stopCaptureSession()
cameraDevice = .back
cameraDevice = .Back
cameraIsSetup = false
previewLayer = nil
captureSession = nil
@ -312,12 +321,15 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
movieOutput = nil
}
/**
Captures still image from currently running capture session.
@available(*, unavailable, renamed="capturePictureWithCompletion")
public func capturePictureWithCompletition(imageCompletition: (UIImage?, NSError?) -> Void) {}
:param: imageCompletion Completion block containing the captured UIImage
*/
open func capturePictureWithCompletion(_ imageCompletion: @escaping (UIImage?, NSError?) -> Void) {
/**
Captures still image from currently running capture session.
:param: imageCompletion Completion block containing the captured UIImage
*/
public func capturePictureWithCompletion(imageCompletion: (UIImage?, NSError?) -> Void) {
self.capturePictureDataWithCompletion { data, error in
guard error == nil, let imageData = data else {
@ -327,49 +339,53 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
if self.writeFilesToPhoneLibrary == true, let library = self.library {
library.writeImageDataToSavedPhotosAlbum(imageData, metadata:nil, completionBlock: { picUrl, error in
library.performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: UIImage(data: imageData)!)
}, completionHandler: { success, error in
guard error != nil else {
return
}
guard error != nil else {
return
}
dispatch_async(dispatch_get_main_queue(), {
self._show(NSLocalizedString("Error", comment:""), message: error.localizedDescription)
})
DispatchQueue.main.async(execute: {
self._show(NSLocalizedString("Error", comment:""), message: (error?.localizedDescription)!)
})
})
}
imageCompletion(UIImage(data: imageData), nil)
}
}
@available(*, unavailable, renamed="capturePictureWithCompletion")
public func capturePictureDataWithCompletition(imageCompletition: (NSData?, NSError?) -> Void) {}
/**
Captures still image from currently running capture session.
:param: imageCompletion Completion block containing the captured imageData
*/
open func capturePictureDataWithCompletion(_ imageCompletion: @escaping (Data?, NSError?) -> Void) {
public func capturePictureDataWithCompletion(imageCompletion: (NSData?, NSError?) -> Void) {
guard cameraIsSetup else {
_show(NSLocalizedString("No capture session setup", comment:""), message: NSLocalizedString("I can't take any picture", comment:""))
return
}
guard cameraOutputMode == .stillImage else {
guard cameraOutputMode == .StillImage else {
_show(NSLocalizedString("Capture session output mode video", comment:""), message: NSLocalizedString("I can't take any picture", comment:""))
return
}
sessionQueue.async(execute: {
self._getStillImageOutput().captureStillImageAsynchronously(from: self._getStillImageOutput().connection(withMediaType: AVMediaTypeVideo), completionHandler: { [unowned self] sample, error in
dispatch_async(sessionQueue, {
self._getStillImageOutput().captureStillImageAsynchronouslyFromConnection(self._getStillImageOutput().connectionWithMediaType(AVMediaTypeVideo), completionHandler: { [unowned self] sample, error in
guard error == nil else {
DispatchQueue.main.async(execute: {
self._show(NSLocalizedString("Error", comment:""), message: (error?.localizedDescription)!)
dispatch_async(dispatch_get_main_queue(), {
self._show(NSLocalizedString("Error", comment:""), message: error.localizedDescription)
})
imageCompletion(nil, error as NSError?)
imageCompletion(nil, error)
return
}
@ -378,28 +394,31 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
imageCompletion(imageData, nil)
})
})
})
}
/**
Starts recording a video with or without voice as in the session preset.
*/
open func startRecordingVideo() {
if cameraOutputMode != .stillImage {
_getMovieOutput().startRecording(toOutputFileURL: tempFilePath, recordingDelegate: self)
Starts recording a video with or without voice as in the session preset.
*/
public func startRecordingVideo() {
if cameraOutputMode != .StillImage {
_getMovieOutput().startRecordingToOutputFileURL(tempFilePath, recordingDelegate: self)
} else {
_show(NSLocalizedString("Capture session output still image", comment:""), message: NSLocalizedString("I can only take pictures", comment:""))
}
}
@available(*, unavailable, renamed="stopVideoRecording")
public func stopRecordingVideo(completition:(videoURL: NSURL?, error: NSError?) -> Void) {}
/**
Stop recording a video. Save it to the cameraRoll and give back the url.
*/
open func stopVideoRecording(_ completion:((_ videoURL: URL?, _ error: NSError?) -> Void)?) {
Stop recording a video. Save it to the cameraRoll and give back the url.
*/
public func stopVideoRecording(completion:(videoURL: NSURL?, error: NSError?) -> Void) {
if let runningMovieOutput = movieOutput {
if runningMovieOutput.isRecording {
if runningMovieOutput.recording {
videoCompletion = completion
runningMovieOutput.stopRecording()
}
@ -407,96 +426,79 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
/**
Current camera status.
Current camera status.
:returns: Current state of the camera: Ready / AccessDenied / NoDeviceFound / NotDetermined
*/
open func currentCameraStatus() -> CameraState {
:returns: Current state of the camera: Ready / AccessDenied / NoDeviceFound / NotDetermined
*/
public func currentCameraStatus() -> CameraState {
return _checkIfCameraIsAvailable()
}
/**
Change current flash mode to next value from available ones.
Change current flash mode to next value from available ones.
:returns: Current flash mode: Off / On / Auto
*/
open func changeFlashMode() -> CameraFlashMode {
:returns: Current flash mode: Off / On / Auto
*/
public func changeFlashMode() -> CameraFlashMode {
flashMode = CameraFlashMode(rawValue: (flashMode.rawValue+1)%3)!
return flashMode
}
/**
Change current output quality mode to next value from available ones.
Change current output quality mode to next value from available ones.
:returns: Current quality mode: Low / Medium / High
*/
open func changeQualityMode() -> CameraOutputQuality {
:returns: Current quality mode: Low / Medium / High
*/
public func changeQualityMode() -> CameraOutputQuality {
cameraOutputQuality = CameraOutputQuality(rawValue: (cameraOutputQuality.rawValue+1)%3)!
return cameraOutputQuality
}
// MARK: - AVCaptureFileOutputRecordingDelegate
open func capture(_ captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAt fileURL: URL!, fromConnections connections: [Any]!) {
public func captureOutput(captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAtURL fileURL: NSURL!, fromConnections connections: [AnyObject]!) {
captureSession?.beginConfiguration()
if flashMode != .off {
if flashMode != .Off {
_updateTorch(flashMode)
}
captureSession?.commitConfiguration()
}
open func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) {
_updateTorch(.off)
public func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
_updateTorch(.Off)
if (error != nil) {
_show(NSLocalizedString("Unable to save video to the iPhone", comment:""), message: error.localizedDescription)
} else {
if writeFilesToPhoneLibrary {
if PHPhotoLibrary.authorizationStatus() == .authorized {
saveVideoToLibrary(outputFileURL)
}
else {
PHPhotoLibrary.requestAuthorization({ (autorizationStatus) in
if autorizationStatus == .authorized {
self.saveVideoToLibrary(outputFileURL)
if let validLibrary = library {
if writeFilesToPhoneLibrary {
validLibrary.writeVideoAtPathToSavedPhotosAlbum(outputFileURL, completionBlock: { (assetURL: NSURL?, error: NSError?) -> Void in
if (error != nil) {
self._show(NSLocalizedString("Unable to save video to the iPhone.", comment:""), message: error!.localizedDescription)
self._executeVideoCompletionWithURL(nil, error: error)
} else {
if let validAssetURL = assetURL {
self._executeVideoCompletionWithURL(validAssetURL, error: error)
}
}
})
} else {
_executeVideoCompletionWithURL(outputFileURL, error: error)
}
} else {
_executeVideoCompletionWithURL(outputFileURL, error: error as NSError?)
}
}
}
fileprivate func saveVideoToLibrary(_ fileURL: URL) {
if let validLibrary = library {
validLibrary.performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: fileURL)
}, completionHandler: { success, error in
if (error != nil) {
self._show(NSLocalizedString("Unable to save video to the iPhone.", comment:""), message: error!.localizedDescription)
self._executeVideoCompletionWithURL(nil, error: error as NSError?)
} else {
self._executeVideoCompletionWithURL(fileURL, error: error as NSError?)
}
})
}
}
// MARK: - UIGestureRecognizerDelegate
fileprivate func attachZoom(_ view: UIView) {
private func attachZoom(view: UIView) {
let pinch = UIPinchGestureRecognizer(target: self, action: #selector(CameraManager._zoomStart(_:)))
view.addGestureRecognizer(pinch)
pinch.delegate = self
}
open func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
public func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer.isKind(of: UIPinchGestureRecognizer.self) {
if gestureRecognizer.isKindOfClass(UIPinchGestureRecognizer) {
beginZoomScale = zoomScale;
}
@ -504,18 +506,18 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
@objc
fileprivate func _zoomStart(_ recognizer: UIPinchGestureRecognizer) {
private func _zoomStart(recognizer: UIPinchGestureRecognizer) {
guard let view = embeddingView,
let previewLayer = previewLayer
previewLayer = previewLayer
else { return }
var allTouchesOnPreviewLayer = true
let numTouch = recognizer.numberOfTouches
let numTouch = recognizer.numberOfTouches()
for i in 0 ..< numTouch {
let location = recognizer.location(ofTouch: i, in: view)
let convertedTouch = previewLayer.convert(location, from: previewLayer.superlayer)
if !previewLayer.contains(convertedTouch) {
let location = recognizer.locationOfTouch(i, inView: view)
let convertedTouch = previewLayer.convertPoint(location, fromLayer: previewLayer.superlayer)
if !previewLayer.containsPoint(convertedTouch) {
allTouchesOnPreviewLayer = false
break
}
@ -525,7 +527,7 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
}
fileprivate func _zoom(_ scale: CGFloat) {
private func _zoom(scale: CGFloat) {
do {
let captureDevice = AVCaptureDevice.devices().first as? AVCaptureDevice
try captureDevice?.lockForConfiguration()
@ -543,12 +545,12 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
// MARK: - CameraManager()
fileprivate func _updateTorch(_ flashMode: CameraFlashMode) {
private func _updateTorch(flashMode: CameraFlashMode) {
captureSession?.beginConfiguration()
let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo)
for device in devices! {
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
for device in devices {
let captureDevice = device as! AVCaptureDevice
if (captureDevice.position == AVCaptureDevicePosition.back) {
if (captureDevice.position == AVCaptureDevicePosition.Back) {
let avTorchMode = AVCaptureTorchMode(rawValue: flashMode.rawValue)
if (captureDevice.isTorchModeSupported(avTorchMode!)) {
do {
@ -565,18 +567,18 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
fileprivate func _executeVideoCompletionWithURL(_ url: URL?, error: NSError?) {
private func _executeVideoCompletionWithURL(url: NSURL?, error: NSError?) {
if let validCompletion = videoCompletion {
validCompletion(url, error)
validCompletion(videoURL: url, error: error)
videoCompletion = nil
}
}
fileprivate func _getMovieOutput() -> AVCaptureMovieFileOutput {
private func _getMovieOutput() -> AVCaptureMovieFileOutput {
var shouldReinitializeMovieOutput = movieOutput == nil
if !shouldReinitializeMovieOutput {
if let connection = movieOutput!.connection(withMediaType: AVMediaTypeVideo) {
shouldReinitializeMovieOutput = shouldReinitializeMovieOutput || !connection.isActive
if let connection = movieOutput!.connectionWithMediaType(AVMediaTypeVideo) {
shouldReinitializeMovieOutput = shouldReinitializeMovieOutput || !connection.active
}
}
@ -595,11 +597,11 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
return movieOutput!
}
fileprivate func _getStillImageOutput() -> AVCaptureStillImageOutput {
private func _getStillImageOutput() -> AVCaptureStillImageOutput {
var shouldReinitializeStillImageOutput = stillImageOutput == nil
if !shouldReinitializeStillImageOutput {
if let connection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo) {
shouldReinitializeStillImageOutput = shouldReinitializeStillImageOutput || !connection.isActive
if let connection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
shouldReinitializeStillImageOutput = shouldReinitializeStillImageOutput || !connection.active
}
}
if shouldReinitializeStillImageOutput {
@ -616,26 +618,26 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
return stillImageOutput!
}
@objc fileprivate func _orientationChanged() {
@objc private func _orientationChanged() {
var currentConnection: AVCaptureConnection?;
switch cameraOutputMode {
case .stillImage:
currentConnection = stillImageOutput?.connection(withMediaType: AVMediaTypeVideo)
case .videoOnly, .videoWithMic:
currentConnection = _getMovieOutput().connection(withMediaType: AVMediaTypeVideo)
case .StillImage:
currentConnection = stillImageOutput?.connectionWithMediaType(AVMediaTypeVideo)
case .VideoOnly, .VideoWithMic:
currentConnection = _getMovieOutput().connectionWithMediaType(AVMediaTypeVideo)
}
if let validPreviewLayer = previewLayer {
if let validPreviewLayerConnection = validPreviewLayer.connection {
if validPreviewLayerConnection.isVideoOrientationSupported {
if validPreviewLayerConnection.supportsVideoOrientation {
validPreviewLayerConnection.videoOrientation = _currentVideoOrientation()
}
}
if let validOutputLayerConnection = currentConnection {
if validOutputLayerConnection.isVideoOrientationSupported {
if validOutputLayerConnection.supportsVideoOrientation {
validOutputLayerConnection.videoOrientation = _currentVideoOrientation()
}
}
DispatchQueue.main.async(execute: { () -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
if let validEmbeddingView = self.embeddingView {
validPreviewLayer.frame = validEmbeddingView.bounds
}
@ -643,26 +645,26 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
}
fileprivate func _currentVideoOrientation() -> AVCaptureVideoOrientation {
switch UIDevice.current.orientation {
case .landscapeLeft:
return .landscapeRight
case .landscapeRight:
return .landscapeLeft
private func _currentVideoOrientation() -> AVCaptureVideoOrientation {
switch UIDevice.currentDevice().orientation {
case .LandscapeLeft:
return .LandscapeRight
case .LandscapeRight:
return .LandscapeLeft
default:
return .portrait
return .Portrait
}
}
fileprivate func _canLoadCamera() -> Bool {
private func _canLoadCamera() -> Bool {
let currentCameraState = _checkIfCameraIsAvailable()
return currentCameraState == .ready || (currentCameraState == .notDetermined && showAccessPermissionPopupAutomatically)
return currentCameraState == .Ready || (currentCameraState == .NotDetermined && showAccessPermissionPopupAutomatically)
}
fileprivate func _setupCamera(_ completion: @escaping (Void) -> Void) {
private func _setupCamera(completion: Void -> Void) {
captureSession = AVCaptureSession()
sessionQueue.async(execute: {
dispatch_async(sessionQueue, {
if let validCaptureSession = self.captureSession {
validCaptureSession.beginConfiguration()
validCaptureSession.sessionPreset = AVCaptureSessionPresetHigh
@ -683,24 +685,24 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
})
}
fileprivate func _startFollowingDeviceOrientation() {
private func _startFollowingDeviceOrientation() {
if shouldRespondToOrientationChanges && !cameraIsObservingDeviceOrientation {
NotificationCenter.default.addObserver(self, selector: #selector(CameraManager._orientationChanged), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CameraManager._orientationChanged), name: UIDeviceOrientationDidChangeNotification, object: nil)
cameraIsObservingDeviceOrientation = true
}
}
fileprivate func _stopFollowingDeviceOrientation() {
private func _stopFollowingDeviceOrientation() {
if cameraIsObservingDeviceOrientation {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIDeviceOrientationDidChangeNotification, object: nil)
cameraIsObservingDeviceOrientation = false
}
}
fileprivate func _addPreviewLayerToView(_ view: UIView) {
private func _addPreviewLayerToView(view: UIView) {
embeddingView = view
attachZoom(view)
DispatchQueue.main.async(execute: { () -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
guard let _ = self.previewLayer else {
return
}
@ -710,54 +712,54 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
})
}
fileprivate func _setupMaxZoomScale() {
private func _setupMaxZoomScale() {
var maxZoom = CGFloat(1.0)
beginZoomScale = CGFloat(1.0)
if cameraDevice == .back {
if cameraDevice == .Back {
maxZoom = (backCameraDevice?.activeFormat.videoMaxZoomFactor)!
}
else if cameraDevice == .front {
else if cameraDevice == .Front {
maxZoom = (frontCameraDevice?.activeFormat.videoMaxZoomFactor)!
}
maxZoomScale = maxZoom
}
fileprivate func _checkIfCameraIsAvailable() -> CameraState {
let deviceHasCamera = UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.rear) || UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.front)
private func _checkIfCameraIsAvailable() -> CameraState {
let deviceHasCamera = UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Rear) || UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front)
if deviceHasCamera {
let authorizationStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
let userAgreedToUseIt = authorizationStatus == .authorized
let authorizationStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
let userAgreedToUseIt = authorizationStatus == .Authorized
if userAgreedToUseIt {
return .ready
} else if authorizationStatus == AVAuthorizationStatus.notDetermined {
return .notDetermined
return .Ready
} else if authorizationStatus == AVAuthorizationStatus.NotDetermined {
return .NotDetermined
} else {
_show(NSLocalizedString("Camera access denied", comment:""), message:NSLocalizedString("You need to go to settings app and grant acces to the camera device to use it.", comment:""))
return .accessDenied
return .AccessDenied
}
} else {
_show(NSLocalizedString("Camera unavailable", comment:""), message:NSLocalizedString("The device does not have a camera.", comment:""))
return .noDeviceFound
return .NoDeviceFound
}
}
fileprivate func _setupOutputMode(_ newCameraOutputMode: CameraOutputMode, oldCameraOutputMode: CameraOutputMode?) {
private func _setupOutputMode(newCameraOutputMode: CameraOutputMode, oldCameraOutputMode: CameraOutputMode?) {
captureSession?.beginConfiguration()
if let cameraOutputToRemove = oldCameraOutputMode {
// remove current setting
switch cameraOutputToRemove {
case .stillImage:
case .StillImage:
if let validStillImageOutput = stillImageOutput {
captureSession?.removeOutput(validStillImageOutput)
}
case .videoOnly, .videoWithMic:
case .VideoOnly, .VideoWithMic:
if let validMovieOutput = movieOutput {
captureSession?.removeOutput(validMovieOutput)
}
if cameraOutputToRemove == .videoWithMic {
if cameraOutputToRemove == .VideoWithMic {
_removeMicInput()
}
}
@ -765,7 +767,7 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
// configure new devices
switch newCameraOutputMode {
case .stillImage:
case .StillImage:
if (stillImageOutput == nil) {
_setupOutputs()
}
@ -776,10 +778,10 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
}
}
case .videoOnly, .videoWithMic:
case .VideoOnly, .VideoWithMic:
captureSession?.addOutput(_getMovieOutput())
if newCameraOutputMode == .videoWithMic {
if newCameraOutputMode == .VideoWithMic {
if let validMic = _deviceInputFromDevice(mic) {
captureSession?.addInput(validMic)
}
@ -790,7 +792,7 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
_orientationChanged()
}
fileprivate func _setupOutputs() {
private func _setupOutputs() {
if (stillImageOutput == nil) {
stillImageOutput = AVCaptureStillImageOutput()
}
@ -799,35 +801,35 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
movieOutput!.movieFragmentInterval = kCMTimeInvalid
}
if library == nil {
library = PHPhotoLibrary.shared()
library = ALAssetsLibrary()
}
}
fileprivate func _setupPreviewLayer() {
private func _setupPreviewLayer() {
if let validCaptureSession = captureSession {
previewLayer = AVCaptureVideoPreviewLayer(session: validCaptureSession)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
}
}
fileprivate func _updateCameraDevice(_ deviceType: CameraDevice) {
private func _updateCameraDevice(deviceType: CameraDevice) {
if let validCaptureSession = captureSession {
validCaptureSession.beginConfiguration()
let inputs = validCaptureSession.inputs as! [AVCaptureInput]
for input in inputs {
if let deviceInput = input as? AVCaptureDeviceInput {
if deviceInput.device == backCameraDevice && cameraDevice == .front {
if deviceInput.device == backCameraDevice && cameraDevice == .Front {
validCaptureSession.removeInput(deviceInput)
break;
} else if deviceInput.device == frontCameraDevice && cameraDevice == .back {
} else if deviceInput.device == frontCameraDevice && cameraDevice == .Back {
validCaptureSession.removeInput(deviceInput)
break;
}
}
}
switch cameraDevice {
case .front:
case .Front:
if hasFrontCamera {
if let validFrontDevice = _deviceInputFromDevice(frontCameraDevice) {
if !inputs.contains(validFrontDevice) {
@ -835,7 +837,7 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
}
}
case .back:
case .Back:
if let validBackDevice = _deviceInputFromDevice(backCameraDevice) {
if !inputs.contains(validBackDevice) {
validCaptureSession.addInput(validBackDevice)
@ -846,12 +848,12 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
}
fileprivate func _updateFlasMode(_ flashMode: CameraFlashMode) {
private func _updateFlasMode(flashMode: CameraFlashMode) {
captureSession?.beginConfiguration()
let devices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo)
for device in devices! {
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
for device in devices {
let captureDevice = device as! AVCaptureDevice
if (captureDevice.position == AVCaptureDevicePosition.back) {
if (captureDevice.position == AVCaptureDevicePosition.Back) {
let avFlashMode = AVCaptureFlashMode(rawValue: flashMode.rawValue)
if (captureDevice.isFlashModeSupported(avFlashMode!)) {
do {
@ -867,16 +869,16 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
captureSession?.commitConfiguration()
}
fileprivate func _updateCameraQualityMode(_ newCameraOutputQuality: CameraOutputQuality) {
private func _updateCameraQualityMode(newCameraOutputQuality: CameraOutputQuality) {
if let validCaptureSession = captureSession {
var sessionPreset = AVCaptureSessionPresetLow
switch (newCameraOutputQuality) {
case CameraOutputQuality.low:
case CameraOutputQuality.Low:
sessionPreset = AVCaptureSessionPresetLow
case CameraOutputQuality.medium:
case CameraOutputQuality.Medium:
sessionPreset = AVCaptureSessionPresetMedium
case CameraOutputQuality.high:
if cameraOutputMode == .stillImage {
case CameraOutputQuality.High:
if cameraOutputMode == .StillImage {
sessionPreset = AVCaptureSessionPresetPhoto
} else {
sessionPreset = AVCaptureSessionPresetHigh
@ -894,7 +896,7 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
}
fileprivate func _removeMicInput() {
private func _removeMicInput() {
guard let inputs = captureSession?.inputs as? [AVCaptureInput] else { return }
for input in inputs {
@ -907,15 +909,15 @@ open class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate, UIGest
}
}
fileprivate func _show(_ title: String, message: String) {
private func _show(title: String, message: String) {
if showErrorsToUsers {
DispatchQueue.main.async(execute: { () -> Void in
self.showErrorBlock(title, message)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.showErrorBlock(erTitle: title, erMessage: message)
})
}
}
fileprivate func _deviceInputFromDevice(_ device: AVCaptureDevice?) -> AVCaptureDeviceInput? {
private func _deviceInputFromDevice(device: AVCaptureDevice?) -> AVCaptureDeviceInput? {
guard let validDevice = device else { return nil }
do {
return try AVCaptureDeviceInput(device: validDevice)

View File

@ -15,7 +15,7 @@ class ImageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = false
self.navigationController?.navigationBar.hidden = false
if let validImage = self.image {
self.imageView.image = validImage

View File

@ -2,12 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSPhotoLibraryUsageDescription</key>
<string>CameraManager will access your photo library to save pictures/video.</string>
<key>NSMicrophoneUsageDescription</key>
<string>CameraManager will use your microphone to record audio for videos.</string>
<key>NSCameraUsageDescription</key>
<string>CameraManager will use your camera to take pictures/video.</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>

View File

@ -33,32 +33,32 @@ class ViewController: UIViewController {
cameraManager.showAccessPermissionPopupAutomatically = false
askForPermissionsButton.isHidden = true
askForPermissionsLabel.isHidden = true
askForPermissionsButton.hidden = true
askForPermissionsLabel.hidden = true
let currentCameraState = cameraManager.currentCameraStatus()
if currentCameraState == .notDetermined {
askForPermissionsButton.isHidden = false
askForPermissionsLabel.isHidden = false
} else if (currentCameraState == .ready) {
if currentCameraState == .NotDetermined {
askForPermissionsButton.hidden = false
askForPermissionsLabel.hidden = false
} else if (currentCameraState == .Ready) {
addCameraToView()
}
if !cameraManager.hasFlash {
flashModeButton.isEnabled = false
flashModeButton.setTitle("No flash", for: UIControlState())
flashModeButton.enabled = false
flashModeButton.setTitle("No flash", forState: UIControlState.Normal)
}
}
override func viewWillAppear(_ animated: Bool) {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.isHidden = true
navigationController?.navigationBar.hidden = true
cameraManager.resumeCaptureSession()
}
override func viewWillDisappear(_ animated: Bool) {
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
cameraManager.stopCaptureSession()
}
@ -66,42 +66,42 @@ class ViewController: UIViewController {
// MARK: - ViewController
fileprivate func addCameraToView()
private func addCameraToView()
{
cameraManager.addPreviewLayerToView(cameraView, newCameraOutputMode: CameraOutputMode.videoWithMic)
cameraManager.addPreviewLayerToView(cameraView, newCameraOutputMode: CameraOutputMode.VideoWithMic)
cameraManager.showErrorBlock = { [weak self] (erTitle: String, erMessage: String) -> Void in
let alertController = UIAlertController(title: erTitle, message: erMessage, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: { (alertAction) -> Void in }))
let alertController = UIAlertController(title: erTitle, message: erMessage, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (alertAction) -> Void in }))
self?.present(alertController, animated: true, completion: nil)
self?.presentViewController(alertController, animated: true, completion: nil)
}
}
// MARK: - @IBActions
@IBAction func changeFlashMode(_ sender: UIButton)
@IBAction func changeFlashMode(sender: UIButton)
{
switch (cameraManager.changeFlashMode()) {
case .off:
sender.setTitle("Flash Off", for: UIControlState())
case .on:
sender.setTitle("Flash On", for: UIControlState())
case .auto:
sender.setTitle("Flash Auto", for: UIControlState())
case .Off:
sender.setTitle("Flash Off", forState: UIControlState.Normal)
case .On:
sender.setTitle("Flash On", forState: UIControlState.Normal)
case .Auto:
sender.setTitle("Flash Auto", forState: UIControlState.Normal)
}
}
@IBAction func recordButtonTapped(_ sender: UIButton) {
@IBAction func recordButtonTapped(sender: UIButton) {
switch (cameraManager.cameraOutputMode) {
case .stillImage:
case .StillImage:
cameraManager.capturePictureWithCompletion({ (image, error) -> Void in
if let errorOccured = error {
self.cameraManager.showErrorBlock("Error occurred", errorOccured.localizedDescription)
self.cameraManager.showErrorBlock(erTitle: "Error occurred", erMessage: errorOccured.localizedDescription)
}
else {
let vc: ImageViewController? = self.storyboard?.instantiateViewController(withIdentifier: "ImageVC") as? ImageViewController
let vc: ImageViewController? = self.storyboard?.instantiateViewControllerWithIdentifier("ImageVC") as? ImageViewController
if let validVC: ImageViewController = vc {
if let capturedImage = image {
validVC.image = capturedImage
@ -110,51 +110,51 @@ class ViewController: UIViewController {
}
}
})
case .videoWithMic, .videoOnly:
sender.isSelected = !sender.isSelected
sender.setTitle(" ", for: UIControlState.selected)
sender.backgroundColor = sender.isSelected ? UIColor.red : UIColor.green
if sender.isSelected {
case .VideoWithMic, .VideoOnly:
sender.selected = !sender.selected
sender.setTitle(" ", forState: UIControlState.Selected)
sender.backgroundColor = sender.selected ? UIColor.redColor() : UIColor.greenColor()
if sender.selected {
cameraManager.startRecordingVideo()
} else {
cameraManager.stopVideoRecording({ (videoURL, error) -> Void in
if let errorOccured = error {
self.cameraManager.showErrorBlock("Error occurred", errorOccured.localizedDescription)
self.cameraManager.showErrorBlock(erTitle: "Error occurred", erMessage: errorOccured.localizedDescription)
}
})
}
}
}
@IBAction func outputModeButtonTapped(_ sender: UIButton) {
@IBAction func outputModeButtonTapped(sender: UIButton) {
cameraManager.cameraOutputMode = cameraManager.cameraOutputMode == CameraOutputMode.videoWithMic ? CameraOutputMode.stillImage : CameraOutputMode.videoWithMic
cameraManager.cameraOutputMode = cameraManager.cameraOutputMode == CameraOutputMode.VideoWithMic ? CameraOutputMode.StillImage : CameraOutputMode.VideoWithMic
switch (cameraManager.cameraOutputMode) {
case .stillImage:
cameraButton.isSelected = false
cameraButton.backgroundColor = UIColor.green
sender.setTitle("Image", for: UIControlState())
case .videoWithMic, .videoOnly:
sender.setTitle("Video", for: UIControlState())
case .StillImage:
cameraButton.selected = false
cameraButton.backgroundColor = UIColor.greenColor()
sender.setTitle("Image", forState: UIControlState.Normal)
case .VideoWithMic, .VideoOnly:
sender.setTitle("Video", forState: UIControlState.Normal)
}
}
@IBAction func changeCameraDevice(_ sender: UIButton) {
@IBAction func changeCameraDevice(sender: UIButton) {
cameraManager.cameraDevice = cameraManager.cameraDevice == CameraDevice.front ? CameraDevice.back : CameraDevice.front
cameraManager.cameraDevice = cameraManager.cameraDevice == CameraDevice.Front ? CameraDevice.Back : CameraDevice.Front
switch (cameraManager.cameraDevice) {
case .front:
sender.setTitle("Front", for: UIControlState())
case .back:
sender.setTitle("Back", for: UIControlState())
case .Front:
sender.setTitle("Front", forState: UIControlState.Normal)
case .Back:
sender.setTitle("Back", forState: UIControlState.Normal)
}
}
@IBAction func askForCameraPermissions(_ sender: UIButton) {
@IBAction func askForCameraPermissions(sender: UIButton) {
cameraManager.askUserForCameraPermission({ permissionGranted in
self.askForPermissionsButton.isHidden = true
self.askForPermissionsLabel.isHidden = true
self.askForPermissionsButton.hidden = true
self.askForPermissionsLabel.hidden = true
self.askForPermissionsButton.alpha = 0
self.askForPermissionsLabel.alpha = 0
if permissionGranted {
@ -163,15 +163,15 @@ class ViewController: UIViewController {
})
}
@IBAction func changeCameraQuality(_ sender: UIButton) {
@IBAction func changeCameraQuality(sender: UIButton) {
switch (cameraManager.changeQualityMode()) {
case .high:
sender.setTitle("High", for: UIControlState())
case .low:
sender.setTitle("Low", for: UIControlState())
case .medium:
sender.setTitle("Medium", for: UIControlState())
case .High:
sender.setTitle("High", forState: UIControlState.Normal)
case .Low:
sender.setTitle("Low", forState: UIControlState.Normal)
case .Medium:
sender.setTitle("Medium", forState: UIControlState.Normal)
}
}
}