Front and back camera selection

This commit is contained in:
nelanelanela 2014-10-13 13:36:21 +01:00
parent 0cbf1dbb61
commit 1dc3d7f6aa
4 changed files with 87 additions and 7 deletions

View File

@ -18,21 +18,38 @@
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="EI2-CK-oqA" userLabel="cameraView">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<color key="backgroundColor" red="0.54901963470000004" green="0.77647066119999997" blue="0.2470588386" alpha="1" colorSpace="deviceRGB"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<color key="tintColor" red="0.54901963470000004" green="0.77647066119999997" blue="0.2470588386" alpha="1" colorSpace="deviceRGB"/>
</view>
<imageView hidden="YES" userInteractionEnabled="NO" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="yyS-3g-UqL">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
</imageView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="sT4-CC-oh5">
<rect key="frame" x="460" y="534" width="124" height="50"/>
<color key="backgroundColor" red="0.98039221759999995" green="0.0078431377190000002" blue="0.52156865600000002" alpha="1" colorSpace="deviceRGB"/>
<constraints>
<constraint firstAttribute="width" constant="124" id="BGe-7V-1jO"/>
<constraint firstAttribute="height" constant="50" id="k4Z-XY-hG9"/>
</constraints>
<state key="normal" title="Change camera">
<color key="titleColor" red="1" green="0.87630701789999998" blue="0.35755069969999997" alpha="1" colorSpace="calibratedRGB"/>
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="changeCameraDevice:" destination="BYZ-38-t0r" eventType="touchUpInside" id="KDu-mr-jHd"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="1" green="0.1062835932" blue="0.122519394" alpha="1" colorSpace="calibratedRGB"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<gestureRecognizers/>
<constraints>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="sT4-CC-oh5" secondAttribute="bottom" constant="16" id="2R7-rR-eRm"/>
<constraint firstItem="yyS-3g-UqL" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="2RH-Jf-TPm"/>
<constraint firstItem="EI2-CK-oqA" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="E1I-Yh-igT"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="yyS-3g-UqL" secondAttribute="bottom" id="INX-TM-tak"/>
<constraint firstAttribute="trailing" secondItem="yyS-3g-UqL" secondAttribute="trailing" id="JEu-x6-qah"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="EI2-CK-oqA" secondAttribute="bottom" id="JW8-WC-69E"/>
<constraint firstAttribute="trailing" secondItem="sT4-CC-oh5" secondAttribute="trailing" constant="16" id="Q92-5b-7Z7"/>
<constraint firstItem="yyS-3g-UqL" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" id="SyJ-SX-ooI"/>
<constraint firstItem="EI2-CK-oqA" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" id="Y9T-eB-SQS"/>
<constraint firstAttribute="trailing" secondItem="EI2-CK-oqA" secondAttribute="trailing" id="m1R-4g-dGU"/>

View File

@ -9,14 +9,52 @@
import UIKit
import AVFoundation
enum CameraDevice {
case Front
case Back
}
private let _singletonSharedInstance = CameraManager()
class CameraManager: NSObject {
var hasFrontCamera = false
var captureSession: AVCaptureSession?
var showErrorBlock:(erTitle: String, erMessage: String) -> Void = { (erTitle: String, erMessage: String) -> Void in
UIAlertView(title: erTitle, message: erMessage, delegate: nil, cancelButtonTitle: "OK").show()
}
var cameraDevice: CameraDevice {
get {
return self.currentCameraDevice
}
set(newCameraDevice) {
if newCameraDevice != self.currentCameraDevice {
self.captureSession?.beginConfiguration()
switch newCameraDevice {
case .Front:
if self.hasFrontCamera {
if let validBackDevice = self.rearCamera? {
self.captureSession?.removeInput(validBackDevice)
}
if let validFrontDevice = self.frontCamera? {
self.captureSession?.addInput(validFrontDevice)
}
}
case .Back:
if let validFrontDevice = self.frontCamera? {
self.captureSession?.removeInput(validFrontDevice)
}
if let validBackDevice = self.rearCamera? {
self.captureSession?.addInput(validBackDevice)
}
}
self.captureSession?.commitConfiguration()
self.currentCameraDevice = newCameraDevice
}
}
}
private var sessionQueue: dispatch_queue_t = dispatch_queue_create("CameraSessionQueue", DISPATCH_QUEUE_SERIAL)
private var frontCamera: AVCaptureInput?
@ -24,6 +62,7 @@ class CameraManager: NSObject {
private var stillImageOutput: AVCaptureStillImageOutput?
private var previewLayer: AVCaptureVideoPreviewLayer?
private var cameraIsSetup = false
private var currentCameraDevice = CameraDevice.Back
private weak var embedingView: UIView?
class var sharedInstance: CameraManager {
@ -31,12 +70,17 @@ class CameraManager: NSObject {
}
deinit {
self.stopCaptureSession()
self.stopAndRemoveCaptureSession()
self._stopFollowingDeviceOrientation()
}
func addPreeviewLayerToView(view: UIView)
{
if let validEmbedingView = self.embedingView? {
if let validPreviewLayer = self.previewLayer? {
validPreviewLayer.removeFromSuperlayer()
}
}
if self.cameraIsSetup {
self._addPreeviewLayerToView(view)
} else {
@ -50,7 +94,19 @@ class CameraManager: NSObject {
{
self.captureSession?.stopRunning()
}
func stopAndRemoveCaptureSession()
{
self.stopCaptureSession()
self.cameraDevice = .Back
self.cameraIsSetup = false
self.previewLayer = nil
self.captureSession = nil
self.frontCamera = nil
self.rearCamera = nil
self.stillImageOutput = nil
}
func capturePictureWithCompletition(imageCompletition: UIImage -> Void)
{
if self.cameraIsSetup {
@ -126,11 +182,11 @@ class CameraManager: NSObject {
private func _addPreeviewLayerToView(view: UIView)
{
self.embedingView = view
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.previewLayer?.frame = view.layer.bounds
view.clipsToBounds = true
view.layer.addSublayer(self.previewLayer)
self.embedingView = view
})
}
@ -158,6 +214,7 @@ class CameraManager: NSObject {
}
if let validVideoFrontDevice = videoFrontDevice? {
self.frontCamera = AVCaptureDeviceInput.deviceInputWithDevice(validVideoFrontDevice, error: &error) as AVCaptureDeviceInput
self.hasFrontCamera = true
}
if let validVideoBackDevice = videoBackDevice? {
self.rearCamera = AVCaptureDeviceInput.deviceInputWithDevice(validVideoBackDevice, error: &error) as AVCaptureDeviceInput

View File

@ -15,13 +15,15 @@ class ViewController: UIViewController {
@IBOutlet weak var cameraView: UIView!
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
override func viewDidLoad()
{
super.viewDidLoad()
self.cameraManager.addPreeviewLayerToView(self.cameraView)
self.imageView.hidden = true
}
@IBAction func viewTapped(sender: UITapGestureRecognizer) {
@IBAction func viewTapped(sender: UITapGestureRecognizer)
{
if self.cameraView.hidden == true {
self.cameraView.hidden = false
self.imageView.hidden = true
@ -33,6 +35,10 @@ class ViewController: UIViewController {
})
}
}
@IBAction func changeCameraDevice(sender: UIButton)
{
self.cameraManager.cameraDevice = self.cameraManager.cameraDevice == CameraDevice.Front ? CameraDevice.Back : CameraDevice.Front
}
}