Reliability improved.

This commit is contained in:
nelanelanela 2014-10-15 13:45:19 +01:00
parent da9071dbc2
commit cb473b4706
5 changed files with 91 additions and 62 deletions

View File

@ -2,22 +2,4 @@
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "camera/CameraManager.swift"
timestampString = "435067907.058213"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "474"
endingLineNumber = "474"
landmarkName = "_addVideoInput()"
landmarkType = "5">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>

View File

@ -24,11 +24,22 @@
<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="496" y="20" width="88" height="33"/>
<color key="backgroundColor" red="0.98039221759999995" green="0.0078431377190000002" blue="0.52156865600000002" alpha="1" colorSpace="deviceRGB"/>
<state key="normal" title="Front">
<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>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="19j-xT-Ttb">
<rect key="frame" x="16" y="20" width="72" height="33"/>
<rect key="frame" x="16" y="20" width="88" height="33"/>
<color key="backgroundColor" red="0.54901963470000004" green="0.77647066119999997" blue="0.2470588386" alpha="1" colorSpace="deviceRGB"/>
<constraints>
<constraint firstAttribute="width" constant="72" id="7mm-eo-7Ng"/>
<constraint firstAttribute="width" constant="88" id="7mm-eo-7Ng"/>
<constraint firstAttribute="height" constant="33" id="jff-rD-TUp"/>
</constraints>
<state key="normal" title="Flash off">
@ -45,6 +56,7 @@
<constraint firstAttribute="height" constant="50" id="Cr1-Jm-NA1"/>
<constraint firstAttribute="width" constant="50" id="kaE-Cr-QzA"/>
</constraints>
<color key="tintColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<state key="normal">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
@ -57,19 +69,14 @@
<action selector="recordButtonTapped:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Ggq-m1-AQB"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="sT4-CC-oh5">
<rect key="frame" x="513" y="20" width="71" height="33"/>
<color key="backgroundColor" red="0.98039221759999995" green="0.0078431377190000002" blue="0.52156865600000002" alpha="1" colorSpace="deviceRGB"/>
<constraints>
<constraint firstAttribute="width" constant="71" id="BGe-7V-1jO"/>
<constraint firstAttribute="height" constant="33" id="k4Z-XY-hG9"/>
</constraints>
<state key="normal" title="Front">
<color key="titleColor" red="1" green="0.87630701789999998" blue="0.35755069969999997" alpha="1" colorSpace="calibratedRGB"/>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="3WP-Xo-FaJ">
<rect key="frame" x="256" y="20" width="88" height="33"/>
<color key="backgroundColor" red="1" green="0.87630701789999998" blue="0.35755069969999997" alpha="1" colorSpace="calibratedRGB"/>
<state key="normal" title="Video">
<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"/>
<action selector="outputModeButtonTapped:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Bgy-rQ-cfF"/>
</connections>
</button>
</subviews>
@ -77,6 +84,8 @@
<gestureRecognizers/>
<constraints>
<constraint firstItem="yyS-3g-UqL" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="2RH-Jf-TPm"/>
<constraint firstItem="19j-xT-Ttb" firstAttribute="height" secondItem="3WP-Xo-FaJ" secondAttribute="height" id="3Fa-ID-yp6"/>
<constraint firstItem="19j-xT-Ttb" firstAttribute="width" secondItem="3WP-Xo-FaJ" secondAttribute="width" id="8aO-Xm-KH2"/>
<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"/>
@ -86,13 +95,18 @@
<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 firstItem="3WP-Xo-FaJ" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="aur-kS-Ri3"/>
<constraint firstAttribute="centerX" secondItem="3WP-Xo-FaJ" secondAttribute="centerX" id="c8T-oR-vKi"/>
<constraint firstAttribute="centerX" secondItem="FSo-n8-ZwV" secondAttribute="centerX" id="dQf-Ne-km4"/>
<constraint firstItem="19j-xT-Ttb" firstAttribute="width" secondItem="sT4-CC-oh5" secondAttribute="width" id="dei-D3-qWh"/>
<constraint firstItem="sT4-CC-oh5" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="i1Z-3m-ytG"/>
<constraint firstAttribute="trailing" secondItem="EI2-CK-oqA" secondAttribute="trailing" id="m1R-4g-dGU"/>
<constraint firstItem="19j-xT-Ttb" firstAttribute="height" secondItem="sT4-CC-oh5" secondAttribute="height" id="mpG-Ir-vxB"/>
<constraint firstItem="19j-xT-Ttb" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" constant="16" id="uuM-vU-IPX"/>
</constraints>
</view>
<connections>
<outlet property="cameraButton" destination="FSo-n8-ZwV" id="ITk-de-2LY"/>
<outlet property="cameraView" destination="EI2-CK-oqA" id="bUS-Rk-omr"/>
<outlet property="imageView" destination="yyS-3g-UqL" id="CZo-cW-NXH"/>
</connections>

View File

@ -31,6 +31,12 @@ enum CameraOutputQuality {
/// Class for handling iDevices custom camera usage
class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
/// Capture sessioc to customize camera settings.
var captureSession: AVCaptureSession?
/// Property to determine if the manager should show the error for the user.
var showErrorsToUsers = true
/// The Bool property to determin if current device has front camera.
var hasFrontCamera: Bool = {
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo)
@ -201,9 +207,6 @@ class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
}
}
/// Capture sessioc to customize camera settings.
var captureSession: AVCaptureSession?
private weak var embedingView: UIView?
private var videoCompletition: ((videoURL: NSURL) -> Void)?
@ -382,19 +385,21 @@ class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
@objc private func _orientationChanged()
{
if let validPreviewLayer = self.previewLayer? {
switch UIDevice.currentDevice().orientation {
case .LandscapeLeft:
validPreviewLayer.connection.videoOrientation = .LandscapeRight
case .LandscapeRight:
validPreviewLayer.connection.videoOrientation = .LandscapeLeft
default:
validPreviewLayer.connection.videoOrientation = .Portrait
}
dispatch_async(dispatch_get_main_queue(), { () -> Void in
if let validEmbedingView = self.embedingView? {
validPreviewLayer.frame = validEmbedingView.bounds
if let validPreviewLayerConnection = validPreviewLayer.connection? {
switch UIDevice.currentDevice().orientation {
case .LandscapeLeft:
validPreviewLayerConnection.videoOrientation = .LandscapeRight
case .LandscapeRight:
validPreviewLayerConnection.videoOrientation = .LandscapeLeft
default:
validPreviewLayerConnection.videoOrientation = .Portrait
}
})
dispatch_async(dispatch_get_main_queue(), { () -> Void in
if let validEmbedingView = self.embedingView? {
validPreviewLayer.frame = validEmbedingView.bounds
}
})
}
}
}
@ -409,9 +414,8 @@ class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
validCaptureSession.beginConfiguration()
self._addVideoInput()
self._setupStillImageOutput()
if let validStillImageOutput = self.stillImageOutput? {
self.captureSession?.addOutput(self.stillImageOutput)
}
self._setupMovieOutput()
self.cameraOutputMode = self.currentCameraOutputMode
self._setupPreviewLayer()
validCaptureSession.commitConfiguration()
validCaptureSession.startRunning()
@ -471,6 +475,9 @@ class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
if let validVideoBackDevice = videoBackDevice? {
self.rearCamera = AVCaptureDeviceInput.deviceInputWithDevice(validVideoBackDevice, error: &error) as AVCaptureDeviceInput
}
if let validError = error? {
self._show("Device setup error occured", message: validError.localizedDescription)
}
self.cameraDevice = self.currentCameraDevice
}
@ -480,7 +487,7 @@ class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
var error: NSError?
let micDevice:AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio);
self.mic = AVCaptureDeviceInput.deviceInputWithDevice(micDevice, error: &error) as? AVCaptureDeviceInput;
if let errorHappened = error {
if let errorHappened = error? {
self.mic = nil
self._show("Mic error", message: errorHappened.description)
}
@ -503,14 +510,18 @@ class CameraManager: NSObject, AVCaptureFileOutputRecordingDelegate {
private func _setupPreviewLayer()
{
self.previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
self.previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
if let validCaptureSession = self.captureSession? {
self.previewLayer = AVCaptureVideoPreviewLayer(session: validCaptureSession)
self.previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
}
}
private func _show (title: String, message: String)
{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.showErrorBlock(erTitle: title, erMessage: message)
})
if self.showErrorsToUsers {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.showErrorBlock(erTitle: title, erMessage: message)
})
}
}
}

View File

@ -14,6 +14,7 @@ class ViewController: UIViewController {
@IBOutlet weak var cameraView: UIView!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var cameraButton: UIButton!
override func viewDidLoad()
{
@ -38,15 +39,36 @@ class ViewController: UIViewController {
@IBAction func recordButtonTapped(sender: UIButton)
{
sender.selected = !sender.selected
sender.backgroundColor = sender.selected ? UIColor.redColor() : UIColor.greenColor()
if sender.selected {
self.cameraManager.startRecordingVideo()
} else {
self.cameraManager.stopRecordingVideo({ (videoURL) -> Void in
println("YEEEEEEY ! ! ")
println(videoURL)
switch (self.cameraManager.cameraOutputMode) {
case .StillImage:
self.cameraManager.capturePictureWithCompletition({ (image) -> Void in
})
case .VideoWithMic, .VideoOnly:
sender.selected = !sender.selected
sender.setTitle(" ", forState: UIControlState.Selected)
sender.backgroundColor = sender.selected ? UIColor.redColor() : UIColor.greenColor()
if sender.selected {
self.cameraManager.startRecordingVideo()
} else {
self.cameraManager.stopRecordingVideo({ (videoURL) -> Void in
println("YEEEEEEY ! ! ")
println(videoURL)
})
}
}
}
@IBAction func outputModeButtonTapped(sender: UIButton)
{
self.cameraManager.cameraOutputMode = self.cameraManager.cameraOutputMode == CameraOutputMode.VideoWithMic ? CameraOutputMode.StillImage : CameraOutputMode.VideoWithMic
switch (self.cameraManager.cameraOutputMode) {
case .StillImage:
self.cameraButton.selected = false
self.cameraButton.backgroundColor = UIColor.greenColor()
sender.setTitle("Image", forState: UIControlState.Normal)
case .VideoWithMic, .VideoOnly:
sender.setTitle("Video", forState: UIControlState.Normal)
}
}