Camera state returned when adding the preview layer.
This commit is contained in:
parent
302cae8b46
commit
5def8cb543
|
|
@ -1,7 +1,7 @@
|
|||
|
||||
####Camera Manager
|
||||
|
||||
V1.0.9 (10-Mar-2015)
|
||||
V1.0.10 (19-Mar-2015)
|
||||
|
||||
####About
|
||||
This is a simple swift class to provide all the configurations you need to create custom camera view in your app.
|
||||
|
|
@ -21,7 +21,7 @@ pod 'CameraManager', '~> 1.0'
|
|||
```
|
||||
|
||||
####How to use
|
||||
To use it you just add the preview layer to your desired view
|
||||
To use it you just add the preview layer to your desired view, you'll get back the state of the camera if it's unavailable, ready or the user denied assess to it.
|
||||
```swift
|
||||
CameraManager.sharedInstance.addPreeviewLayerToView(self.cameraView)
|
||||
```
|
||||
|
|
|
|||
|
|
@ -12,6 +12,10 @@ import AssetsLibrary
|
|||
|
||||
private let _singletonSharedInstance = CameraManager()
|
||||
|
||||
public enum CameraState {
|
||||
case Ready, AccessDenied, NoDeviceFound
|
||||
}
|
||||
|
||||
public enum CameraDevice {
|
||||
case Front, Back
|
||||
}
|
||||
|
|
@ -209,27 +213,33 @@ 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
|
||||
|
||||
:returns: Current state of the camera: Ready / AccessDenied / NoDeviceFound.
|
||||
*/
|
||||
public func addPreviewLayerToView(view: UIView)
|
||||
public func addPreviewLayerToView(view: UIView) -> CameraState
|
||||
{
|
||||
self.addPreviewLayerToView(view, newCameraOutputMode: _cameraOutputMode)
|
||||
return self.addPreviewLayerToView(view, newCameraOutputMode: _cameraOutputMode)
|
||||
}
|
||||
public func addPreviewLayerToView(view: UIView, newCameraOutputMode: CameraOutputMode)
|
||||
public func addPreviewLayerToView(view: UIView, newCameraOutputMode: CameraOutputMode) -> CameraState
|
||||
{
|
||||
if let validEmbedingView = self.embedingView? {
|
||||
if let validPreviewLayer = self.previewLayer? {
|
||||
validPreviewLayer.removeFromSuperlayer()
|
||||
let currentCameraState = _checkIfCameraIsAvailable()
|
||||
if currentCameraState == .Ready {
|
||||
if let validEmbedingView = self.embedingView? {
|
||||
if let validPreviewLayer = self.previewLayer? {
|
||||
validPreviewLayer.removeFromSuperlayer()
|
||||
}
|
||||
}
|
||||
}
|
||||
if self.cameraIsSetup {
|
||||
self._addPreeviewLayerToView(view)
|
||||
self.cameraOutputMode = newCameraOutputMode
|
||||
} else {
|
||||
self._setupCamera({ Void -> Void in
|
||||
if self.cameraIsSetup {
|
||||
self._addPreeviewLayerToView(view)
|
||||
self.cameraOutputMode = newCameraOutputMode
|
||||
})
|
||||
} else {
|
||||
self._setupCamera({ Void -> Void in
|
||||
self._addPreeviewLayerToView(view)
|
||||
self.cameraOutputMode = newCameraOutputMode
|
||||
})
|
||||
}
|
||||
}
|
||||
return currentCameraState
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -471,7 +481,7 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
|
|||
|
||||
private func _setupCamera(completition: Void -> Void)
|
||||
{
|
||||
if self._checkIfCameraIsAvailable() {
|
||||
if self._checkIfCameraIsAvailable() == .Ready {
|
||||
self.captureSession = AVCaptureSession()
|
||||
|
||||
dispatch_async(sessionQueue, {
|
||||
|
|
@ -492,8 +502,6 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
|
|||
completition()
|
||||
}
|
||||
})
|
||||
} else {
|
||||
self._show("Camera unavailable", message: "The device does not have a camera")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -523,10 +531,19 @@ public class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
|
|||
})
|
||||
}
|
||||
|
||||
private func _checkIfCameraIsAvailable() -> Bool
|
||||
private func _checkIfCameraIsAvailable() -> CameraState
|
||||
{
|
||||
let deviceHasCamera = UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Rear) || UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDevice.Front)
|
||||
return deviceHasCamera
|
||||
if deviceHasCamera {
|
||||
let userAgreedToUseIt = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) == .Authorized
|
||||
if userAgreedToUseIt {
|
||||
return .Ready
|
||||
} else {
|
||||
return .AccessDenied
|
||||
}
|
||||
} else {
|
||||
return .NoDeviceFound
|
||||
}
|
||||
}
|
||||
|
||||
private func _addVideoInput()
|
||||
|
|
|
|||
|
|
@ -25,7 +25,12 @@ class ViewController: UIViewController {
|
|||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
self.cameraManager.addPreviewLayerToView(self.cameraView, newCameraOutputMode: CameraOutputMode.StillImage)
|
||||
let currentCameraState = self.cameraManager.addPreviewLayerToView(self.cameraView, newCameraOutputMode: CameraOutputMode.VideoOnly)
|
||||
if currentCameraState == .AccessDenied {
|
||||
UIAlertView(title: "Camera access denied", message: "You need to go to settings app and grant acces to the camera device to use it.", delegate: nil, cancelButtonTitle: "OK").show()
|
||||
} else if (currentCameraState == .NoDeviceFound) {
|
||||
UIAlertView(title: "Camera unavailable", message: "The device does not have a camera.", delegate: nil, cancelButtonTitle: "OK").show()
|
||||
}
|
||||
|
||||
self.cameraManager.cameraDevice = .Front
|
||||
self.imageView.hidden = true
|
||||
|
|
|
|||
Loading…
Reference in New Issue