Merge pull request #21 from olivanov/orientation

Maximum zoom scale & photo/video orientation set from the device
This commit is contained in:
Andrew Walz 2017-03-23 09:24:44 -06:00 committed by GitHub
commit a48222cc0d
4 changed files with 1187 additions and 1121 deletions

View file

@ -125,7 +125,7 @@
TargetAttributes = {
1675A9711E00A68300B80903 = {
CreatedOnToolsVersion = 8.1;
DevelopmentTeam = LW28KCU8N5;
DevelopmentTeam = RZ5847N782;
ProvisioningStyle = Automatic;
};
};
@ -297,7 +297,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = LW28KCU8N5;
DEVELOPMENT_TEAM = RZ5847N782;
INFOPLIST_FILE = DemoSwiftyCam/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
@ -311,7 +311,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = LW28KCU8N5;
DEVELOPMENT_TEAM = RZ5847N782;
INFOPLIST_FILE = DemoSwiftyCam/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";

View file

@ -36,6 +36,7 @@ class PhotoViewController: UIViewController {
super.viewDidLoad()
self.view.backgroundColor = UIColor.gray
let backgroundImageView = UIImageView(frame: view.frame)
backgroundImageView.contentMode = UIViewContentMode.scaleAspectFit
backgroundImageView.image = backgroundImage
view.addSubview(backgroundImageView)
let cancelButton = UIButton(frame: CGRect(x: 10.0, y: 10.0, width: 30.0, height: 30.0))

View file

@ -26,6 +26,7 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate {
super.viewDidLoad()
cameraDelegate = self
maximumVideoDuration = 10.0
shouldUseDeviceOrientation = true
addButtons()
}

View file

@ -109,6 +109,10 @@ open class SwiftyCamViewController: UIViewController {
public var pinchToZoom = true
/// Sets the maximum zoom scale allowed during Pinch gesture
fileprivate var maxZoomScale = CGFloat(4.0)
/// Sets whether Tap to Focus and Tap to Adjust Exposure is enabled for the capture session
public var tapToFocus = true
@ -131,6 +135,10 @@ open class SwiftyCamViewController: UIViewController {
public var defaultCamera = CameraSelection.rear
/// Sets wether the taken photo or video should be oriented according to the device orientation
public var shouldUseDeviceOrientation = false
// MARK: Public Get-only Variable Declarations
@ -202,6 +210,10 @@ open class SwiftyCamViewController: UIViewController {
fileprivate var flashView : UIView?
/// Last changed orientation
fileprivate var deviceOrientation : UIDeviceOrientation?
/// Disable view autorotation for forced portrait recorindg
@ -259,6 +271,12 @@ open class SwiftyCamViewController: UIViewController {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// Subscribe to device rotation notifications
if shouldUseDeviceOrientation {
subscribeToDeviceOrientationChangeNotifications()
}
// Set background audio preference
setBackgroundAudioPreference()
@ -300,6 +318,11 @@ open class SwiftyCamViewController: UIViewController {
//Disble flash if it is currently enabled
disableFlash()
// Unsubscribe from device rotation notifications
if shouldUseDeviceOrientation {
unsubscribeFromDeviceOrientationChangeNotifications()
}
}
// MARK: Public Functions
@ -372,8 +395,6 @@ open class SwiftyCamViewController: UIViewController {
previewLayer.addSubview(flashView!)
}
let videoPreviewLayerOrientation = previewLayer!.videoPreviewLayer.connection.videoOrientation
sessionQueue.async { [unowned self] in
if !movieFileOutput.isRecording {
if UIDevice.current.isMultitaskingSupported {
@ -388,7 +409,8 @@ open class SwiftyCamViewController: UIViewController {
if self.currentCamera == .front {
movieFileOutputConnection?.isVideoMirrored = true
}
movieFileOutputConnection?.videoOrientation = videoPreviewLayerOrientation
movieFileOutputConnection?.videoOrientation = self.getVideoOrientation()
// Start recording to a temporary file.
let outputFileName = UUID().uuidString
@ -636,6 +658,54 @@ open class SwiftyCamViewController: UIViewController {
}
}
/// Orientation management
fileprivate func subscribeToDeviceOrientationChangeNotifications() {
self.deviceOrientation = UIDevice.current.orientation
NotificationCenter.default.addObserver(self, selector: #selector(deviceDidRotate), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
fileprivate func unsubscribeFromDeviceOrientationChangeNotifications() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
self.deviceOrientation = nil
}
@objc fileprivate func deviceDidRotate() {
if !UIDevice.current.orientation.isFlat {
self.deviceOrientation = UIDevice.current.orientation
}
}
fileprivate func getVideoOrientation() -> AVCaptureVideoOrientation {
guard shouldUseDeviceOrientation, let deviceOrientation = self.deviceOrientation else { return previewLayer!.videoPreviewLayer.connection.videoOrientation }
switch deviceOrientation {
case .landscapeLeft:
return .landscapeRight
case .landscapeRight:
return .landscapeLeft
case .portraitUpsideDown:
return .portraitUpsideDown
default:
return .portrait
}
}
fileprivate func getImageOrientation(forCamera: CameraSelection) -> UIImageOrientation {
guard shouldUseDeviceOrientation, let deviceOrientation = self.deviceOrientation else { return forCamera == .rear ? .right : .leftMirrored }
switch deviceOrientation {
case .landscapeLeft:
return forCamera == .rear ? .up : .downMirrored
case .landscapeRight:
return forCamera == .rear ? .down : .upMirrored
case .portraitUpsideDown:
return forCamera == .rear ? .left : .rightMirrored
default:
return forCamera == .rear ? .right : .leftMirrored
}
}
/**
Returns a UIImage from Image Data.
@ -648,23 +718,17 @@ open class SwiftyCamViewController: UIViewController {
let dataProvider = CGDataProvider(data: imageData as CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)
var image: UIImage!
// Set proper orientation for photo
// If camera is currently set to front camera, flip image
switch self.currentCamera {
case .front:
image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: .leftMirrored)
case .rear:
image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: .right)
}
let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: self.getImageOrientation(forCamera: self.currentCamera))
return image
}
fileprivate func capturePhotoAsyncronously(completionHandler: @escaping(Bool) -> ()) {
if let videoConnection = photoFileOutput?.connection(withMediaType: AVMediaTypeVideo) {
videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait
photoFileOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(sampleBuffer, error) in
if (sampleBuffer != nil) {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
@ -898,7 +962,7 @@ extension SwiftyCamViewController {
let captureDevice = AVCaptureDevice.devices().first as? AVCaptureDevice
try captureDevice?.lockForConfiguration()
zoomScale = max(1.0, min(beginZoomScale * pinch.scale, captureDevice!.activeFormat.videoMaxZoomFactor))
zoomScale = min(maxZoomScale, max(1.0, min(beginZoomScale * pinch.scale, captureDevice!.activeFormat.videoMaxZoomFactor)))
captureDevice?.videoZoomFactor = zoomScale