diff --git a/camera.xcodeproj/project.xcworkspace/xcuserdata/nataliaterlecka.xcuserdatad/UserInterfaceState.xcuserstate b/camera.xcodeproj/project.xcworkspace/xcuserdata/nataliaterlecka.xcuserdatad/UserInterfaceState.xcuserstate index d490e12..84bd0c8 100644 Binary files a/camera.xcodeproj/project.xcworkspace/xcuserdata/nataliaterlecka.xcuserdatad/UserInterfaceState.xcuserstate and b/camera.xcodeproj/project.xcworkspace/xcuserdata/nataliaterlecka.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/camera.xcodeproj/xcuserdata/nataliaterlecka.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/camera.xcodeproj/xcuserdata/nataliaterlecka.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 6e00952..fe2b454 100644 --- a/camera.xcodeproj/xcuserdata/nataliaterlecka.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/camera.xcodeproj/xcuserdata/nataliaterlecka.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -2,22 +2,4 @@ - - - - - - diff --git a/camera/Base.lproj/Main.storyboard b/camera/Base.lproj/Main.storyboard index fbff775..99d6bf1 100644 --- a/camera/Base.lproj/Main.storyboard +++ b/camera/Base.lproj/Main.storyboard @@ -24,11 +24,22 @@ + - @@ -77,6 +84,8 @@ + + @@ -86,13 +95,18 @@ + + + + + diff --git a/camera/CameraManager.swift b/camera/CameraManager.swift index 2a27bb9..2dc0132 100644 --- a/camera/CameraManager.swift +++ b/camera/CameraManager.swift @@ -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) + }) + } } } diff --git a/camera/ViewController.swift b/camera/ViewController.swift index 1c7179d..e9dd6ff 100644 --- a/camera/ViewController.swift +++ b/camera/ViewController.swift @@ -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) } }