cameraIsReady property and addPreviewLayerToView competition block.

This commit is contained in:
Natalia Terlecka 2015-12-24 09:52:39 +01:00
parent a10dc6338c
commit eb4e77d2bb
2 changed files with 35 additions and 10 deletions

View File

@ -68,7 +68,14 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
}
}
}
/// The Bool property to determine if the camera is ready to use.
public var cameraIsReady: Bool {
get {
return cameraIsSetup
}
}
/// The Bool property to determine if current device has front camera.
public var hasFrontCamera: Bool = {
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
@ -79,7 +86,7 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
}
}
return false
}()
}()
/// The Bool property to determine if current device has flash.
public var hasFlash: Bool = {
@ -96,8 +103,10 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
/// Property to change camera device between front and back.
public var cameraDevice = CameraDevice.Back {
didSet {
if cameraDevice != oldValue {
_updateCameraDevice(cameraDevice)
if cameraIsSetup {
if cameraDevice != oldValue {
_updateCameraDevice(cameraDevice)
}
}
}
}
@ -105,8 +114,10 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
/// Property to change camera flash mode.
public var flashMode = CameraFlashMode.Off {
didSet {
if flashMode != oldValue {
_updateFlasMode(flashMode)
if cameraIsSetup {
if flashMode != oldValue {
_updateFlasMode(flashMode)
}
}
}
}
@ -114,8 +125,10 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
/// Property to change camera output quality.
public var cameraOutputQuality = CameraOutputQuality.High {
didSet {
if cameraOutputQuality != oldValue {
_updateCameraQualityMode(cameraOutputQuality)
if cameraIsSetup {
if cameraOutputQuality != oldValue {
_updateCameraQualityMode(cameraOutputQuality)
}
}
}
}
@ -123,8 +136,10 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
/// Property to change camera output.
public var cameraOutputMode = CameraOutputMode.StillImage {
didSet {
if cameraOutputMode != oldValue {
_setupOutputMode(cameraOutputMode, oldCameraOutputMode: oldValue)
if cameraIsSetup {
if cameraOutputMode != oldValue {
_setupOutputMode(cameraOutputMode, oldCameraOutputMode: oldValue)
}
}
}
}
@ -183,6 +198,7 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
: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: completition Optional completition block
:returns: Current state of the camera: Ready / AccessDenied / NoDeviceFound / NotDetermined.
*/
@ -190,6 +206,9 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
return addPreviewLayerToView(view, newCameraOutputMode: cameraOutputMode)
}
public func addPreviewLayerToView(view: UIView, newCameraOutputMode: CameraOutputMode) -> CameraState {
return addPreviewLayerToView(view, newCameraOutputMode: cameraOutputMode, completition: nil)
}
public func addPreviewLayerToView(view: UIView, newCameraOutputMode: CameraOutputMode, completition: (Void -> Void)?) -> CameraState {
if _canLoadCamera() {
if let _ = embedingView {
if let validPreviewLayer = previewLayer {
@ -199,10 +218,16 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
if cameraIsSetup {
_addPreviewLayerToView(view)
cameraOutputMode = newCameraOutputMode
if let validCompletition = completition {
validCompletition()
}
} else {
_setupCamera({ Void -> Void in
self._addPreviewLayerToView(view)
self.cameraOutputMode = newCameraOutputMode
if let validCompletition = completition {
validCompletition()
}
})
}
}