Commit 43e1c383 by Daniel Dahan

final touches before README and release of CaptureView

parent 996a1254
......@@ -121,26 +121,40 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
:name: captureSessionWillSwitchCameras
*/
func captureSessionWillSwitchCameras(capture: CaptureSession, position: AVCaptureDevicePosition) {
if .Back == position {
let img: UIImage? = UIImage(named: "ic_flash_off_white")
captureView.captureSession.flashMode = .Off
flashButton.setImage(img, forState: .Normal)
flashButton.setImage(img, forState: .Highlighted)
}
// ... do something
}
/**
:name: captureSessionDidSwitchCamerapublic s
:name: captureSessionDidSwitchCameras
*/
func captureSessionDidSwitchCameras(capture: CaptureSession, position: AVCaptureDevicePosition) {
var img: UIImage?
if .Back == position {
let img: UIImage? = UIImage(named: "ic_flash_auto_white")
captureView.captureSession.flashMode = .Auto
img = UIImage(named: "ic_flash_auto_white")
flashButton.setImage(img, forState: .Normal)
flashButton.setImage(img, forState: .Highlighted)
img = UIImage(named: "ic_camera_front_white")
switchCamerasButton.setImage(img, forState: .Normal)
switchCamerasButton.setImage(img, forState: .Highlighted)
} else {
captureView.captureSession.flashMode = .Off
img = UIImage(named: "ic_flash_off_white")
flashButton.setImage(img, forState: .Normal)
flashButton.setImage(img, forState: .Highlighted)
img = UIImage(named: "ic_camera_rear_white")
switchCamerasButton.setImage(img, forState: .Normal)
switchCamerasButton.setImage(img, forState: .Highlighted)
}
}
/**
:name: captureViewDidPressFlashButton
*/
func captureViewDidPressFlashButton(captureView: CaptureView, button: UIButton) {
if .Back == captureView.captureSession.cameraPosition {
var img: UIImage?
......@@ -191,16 +205,7 @@ class ViewController: UIViewController, CaptureViewDelegate, CaptureSessionDeleg
:name: captureViewDidPressSwitchCamerasButton
*/
func captureViewDidPressSwitchCamerasButton(captureView: CaptureView, button: UIButton) {
var img: UIImage?
if .Back == captureView.captureSession.cameraPosition {
img = UIImage(named: "ic_camera_front_white")
} else if .Front == captureView.captureSession.cameraPosition {
img = UIImage(named: "ic_camera_rear_white")
}
switchCamerasButton.setImage(img, forState: .Normal)
switchCamerasButton.setImage(img, forState: .Highlighted)
// ... do something
}
/**
......
......@@ -53,7 +53,6 @@ public class CapturePreviewView : MaterialView {
:name: preparePreviewLayer
*/
private func preparePreviewLayer() {
layer.addAnimation(MaterialAnimation.transition(.Fade), forKey: kCATransition)
layer.backgroundColor = MaterialColor.black.CGColor
layer.masksToBounds = true
(layer as! AVCaptureVideoPreviewLayer).videoGravity = AVLayerVideoGravityResizeAspectFill
......
......@@ -214,35 +214,35 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
:name: caneraSupportsTapToFocus
*/
public var cameraSupportsTapToFocus: Bool {
return activeCamera!.focusPointOfInterestSupported
return nil == activeCamera ? false : activeCamera!.focusPointOfInterestSupported
}
/**
:name: cameraSupportsTapToExpose
*/
public var cameraSupportsTapToExpose: Bool {
return activeCamera!.exposurePointOfInterestSupported
return nil == activeCamera ? false : activeCamera!.exposurePointOfInterestSupported
}
/**
:name: cameraHasFlash
*/
public var cameraHasFlash: Bool {
return activeCamera!.hasFlash
return nil == activeCamera ? false : activeCamera!.hasFlash
}
/**
:name: cameraHasTorch
*/
public var cameraHasTorch: Bool {
return activeCamera!.hasTorch
return nil == activeCamera ? false : activeCamera!.hasTorch
}
/**
:name: cameraPosition
*/
public var cameraPosition: AVCaptureDevicePosition {
return activeCamera!.position
public var cameraPosition: AVCaptureDevicePosition? {
return activeCamera?.position
}
/**
......@@ -392,10 +392,11 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
/**
:name: switchCameras
*/
public func switchCameras(completion: ((success: Bool) -> Void)? = nil) {
public func switchCameras() {
if canSwitchCameras {
do {
self.delegate?.captureSessionWillSwitchCameras?(self, position: self.cameraPosition)
if let v: AVCaptureDevicePosition = self.cameraPosition {
self.delegate?.captureSessionWillSwitchCameras?(self, position: v)
let videoInput: AVCaptureDeviceInput? = try AVCaptureDeviceInput(device: self.inactiveCamera!)
self.session.beginConfiguration()
self.session.removeInput(self.activeVideoInput)
......@@ -407,10 +408,9 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
self.session.addInput(self.activeVideoInput)
}
self.session.commitConfiguration()
completion?(success: true)
self.delegate?.captureSessionDidSwitchCameras?(self, position: self.cameraPosition)
self.delegate?.captureSessionDidSwitchCameras?(self, position: self.cameraPosition!)
}
} catch let e as NSError {
completion?(success: false)
self.delegate?.captureSessionFailedWithError?(self, error: e)
}
}
......@@ -545,9 +545,9 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
*/
public func captureStillImage() {
dispatch_async(sessionQueue) {
let connection: AVCaptureConnection = self.imageOutput.connectionWithMediaType(AVMediaTypeVideo)
connection.videoOrientation = self.currentVideoOrientation
self.imageOutput.captureStillImageAsynchronouslyFromConnection(connection) { (sampleBuffer: CMSampleBuffer!, error: NSError!) -> Void in
if let v: AVCaptureConnection = self.imageOutput.connectionWithMediaType(AVMediaTypeVideo) {
v.videoOrientation = self.currentVideoOrientation
self.imageOutput.captureStillImageAsynchronouslyFromConnection(v) { (sampleBuffer: CMSampleBuffer!, error: NSError!) -> Void in
if nil == error {
let data: NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
self.delegate?.captureStillImageAsynchronously?(self, image: UIImage(data: data)!)
......@@ -557,6 +557,7 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
}
}
}
}
/**
:name: startRecording
......@@ -564,16 +565,16 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
public func startRecording() {
if !isRecording {
dispatch_async(sessionQueue) {
let connection: AVCaptureConnection = self.movieOutput.connectionWithMediaType(AVMediaTypeVideo)
connection.videoOrientation = self.currentVideoOrientation
connection.preferredVideoStabilizationMode = .Auto
let device: AVCaptureDevice = self.activeCamera!
if device.smoothAutoFocusSupported {
if let v: AVCaptureConnection = self.movieOutput.connectionWithMediaType(AVMediaTypeVideo) {
v.videoOrientation = self.currentVideoOrientation
v.preferredVideoStabilizationMode = .Auto
}
if let v: AVCaptureDevice = self.activeCamera {
if v.smoothAutoFocusSupported {
do {
try device.lockForConfiguration()
device.smoothAutoFocusEnabled = true
device.unlockForConfiguration()
try v.lockForConfiguration()
v.smoothAutoFocusEnabled = true
v.unlockForConfiguration()
} catch let e as NSError {
self.delegate?.captureSessionFailedWithError?(self, error: e)
}
......@@ -586,6 +587,16 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
}
}
}
}
/**
:name: stopRecording
*/
public func stopRecording() {
if isRecording {
movieOutput.stopRecording()
}
}
/**
:name: captureOutput
......@@ -604,15 +615,6 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
}
/**
:name: stopRecording
*/
public func stopRecording() {
if isRecording {
movieOutput.stopRecording()
}
}
/**
:name: prepareSession
*/
private func prepareSession() {
......@@ -666,12 +668,6 @@ public class CaptureSession : NSObject, AVCaptureFileOutputRecordingDelegate {
private func prepareMovieOutput() {
if session.canAddOutput(movieOutput) {
session.addOutput(movieOutput)
// By calling this, it removes the stutter that occurs
// when calling the record button.
let connection: AVCaptureConnection = self.movieOutput.connectionWithMediaType(AVMediaTypeVideo)
connection.videoOrientation = self.currentVideoOrientation
connection.preferredVideoStabilizationMode = .Auto
}
}
......
......@@ -104,6 +104,11 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
private var tapToResetGesture: UITapGestureRecognizer?
/**
:name: captureMode
*/
public lazy var captureMode: CaptureMode = .Video
/**
:name: tapToFocusEnabled
*/
public var tapToFocusEnabled: Bool = false {
......@@ -146,7 +151,7 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
/**
:name: tapToResetEnabled
*/
private var tapToResetEnabled: Bool = false {
public var tapToResetEnabled: Bool = false {
didSet {
if tapToResetEnabled {
prepareResetLayer()
......@@ -166,11 +171,6 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
}
/**
:name: captureMode
*/
public private(set) lazy var captureMode: CaptureMode = .Video
/**
:name: contentInsets
*/
public var contentInsets: MaterialEdgeInsets = .None {
......@@ -284,6 +284,8 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
*/
public override func layoutSubviews() {
super.layoutSubviews()
previewView.frame = bounds
if let v: UIButton = cameraButton {
v.frame.origin.y = bounds.height - contentInsetsRef.bottom - v.bounds.height
v.frame.origin.x = contentInsetsRef.left
......@@ -296,7 +298,9 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
v.frame.origin.y = bounds.height - contentInsetsRef.bottom - v.bounds.height
v.frame.origin.x = bounds.width - v.bounds.width - contentInsetsRef.right
}
(previewView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation = captureSession.currentVideoOrientation
if let v: AVCaptureConnection = (previewView.layer as! AVCaptureVideoPreviewLayer).connection {
v.videoOrientation = captureSession.currentVideoOrientation
}
}
/**
......@@ -331,7 +335,6 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
}
insertSubview(previewView, atIndex: 0)
MaterialLayout.alignToParent(self, child: previewView)
if let v: UIButton = captureButton {
insertSubview(v, atIndex: 1)
......@@ -393,10 +396,8 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
:name: handleSwitchCamerasButton
*/
internal func handleSwitchCamerasButton(button: UIButton) {
previewView.layer.addAnimation(MaterialAnimation.transition(.Fade), forKey: kCATransition)
captureSession.switchCameras { (success: Bool) in
(self.delegate as? CaptureViewDelegate)?.captureViewDidPressSwitchCamerasButton?(self, button: button)
}
captureSession.switchCameras()
(delegate as? CaptureViewDelegate)?.captureViewDidPressSwitchCamerasButton?(self, button: button)
}
/**
......@@ -410,7 +411,6 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
captureSession.stopRecording()
stopTimer()
} else {
previewView.layer.addAnimation(MaterialAnimation.transition(.Fade), forKey: kCATransition)
captureSession.startRecording()
startTimer()
}
......@@ -496,7 +496,6 @@ public class CaptureView : MaterialView, UIGestureRecognizerDelegate {
:name: preparePreviewView
*/
private func preparePreviewView() {
previewView.translatesAutoresizingMaskIntoConstraints = false
(previewView.layer as! AVCaptureVideoPreviewLayer).session = captureSession.session
captureSession.startSession()
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment