From 7855da4ec7f8250daac82a27a05448f2232d3d62 Mon Sep 17 00:00:00 2001 From: Kevin Date: Thu, 7 May 2020 12:37:34 -0400 Subject: [PATCH 1/2] Adding the ability to display the previewLayer in a view set by the subclassing ViewController --- README.md | 14 ++++++++++++++ Source/SwiftyCamViewController.swift | 14 +++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6962b55..a324b35 100644 --- a/README.md +++ b/README.md @@ -314,6 +314,20 @@ tapToFocus = false By default, `tapToFocus` is enabled. If you wish to show a on screen animation when a tap to focus is initiated, you can use the `SwiftyCamDidFocusAtPoint(focusPoint:)` to get the coordinates of tap and provide your own tap animation +## Camera Display + +By default, SwiftyCam displays the capture session to fit the entire screen. If you wish to fit it to your own view: + +```swift +class MyCameraViewController : SwiftyCamViewController, SwiftyCamViewControllerDelegate { + + override func getPreviewViewParentView() -> UIView { + return customView + } + ... +} +``` + ## Device Orientation By default, SwiftyCam will set the photo orientation to be portrait. If you wish to preserve the orientation of the capture photos to allow support for landscape images, use the `shouldUseDeviceOrientation` property: diff --git a/Source/SwiftyCamViewController.swift b/Source/SwiftyCamViewController.swift index 1497a33..0680e76 100644 --- a/Source/SwiftyCamViewController.swift +++ b/Source/SwiftyCamViewController.swift @@ -298,15 +298,19 @@ open class SwiftyCamViewController: UIViewController { public var videoCodecType: AVVideoCodecType? = nil // MARK: ViewDidLoad - + /// ViewDidLoad Implementation + open func getPreviewViewParentView() -> UIView { + return view + } override open func viewDidLoad() { super.viewDidLoad() - previewLayer = PreviewView(frame: view.frame, videoGravity: videoGravity) - previewLayer.center = view.center - view.addSubview(previewLayer) - view.sendSubviewToBack(previewLayer) + let previewParentView = self.getPreviewViewParentView() + previewLayer = PreviewView(frame: previewParentView.bounds, videoGravity: videoGravity) + previewLayer.center = CGPoint(x: previewParentView.bounds.midX, y: previewParentView.bounds.midY) + previewParentView.addSubview(previewLayer) + previewParentView.sendSubviewToBack(previewLayer) // Add Gesture Recognizers From c5b2bed7f969401e16313912bf2f0e573d7bb71f Mon Sep 17 00:00:00 2001 From: Kevin Date: Sat, 9 May 2020 12:47:51 -0400 Subject: [PATCH 2/2] missed two things --- DemoSwiftyCam/DemoSwiftyCam/ViewController.swift | 9 ++++++++- Source/SwiftyCamViewController.swift | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift b/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift index bd1f19d..22e3901 100644 --- a/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift +++ b/DemoSwiftyCam/DemoSwiftyCam/ViewController.swift @@ -22,6 +22,13 @@ class ViewController: SwiftyCamViewController, SwiftyCamViewControllerDelegate { @IBOutlet weak var captureButton : SwiftyRecordButton! @IBOutlet weak var flipCameraButton : UIButton! @IBOutlet weak var flashButton : UIButton! + + override func getPreviewViewParentView() -> UIView { + let view = UIView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 300, height: 300))) + view.center = self.view.center + self.view.addSubview(view) + return view + } override func viewDidLoad() { super.viewDidLoad() @@ -136,7 +143,7 @@ extension ViewController { let focusView = UIImageView(image: #imageLiteral(resourceName: "focus")) focusView.center = point focusView.alpha = 0.0 - view.addSubview(focusView) + self.getPreviewViewParentView().addSubview(focusView) UIView.animate(withDuration: 0.25, delay: 0.0, options: .curveEaseInOut, animations: { focusView.alpha = 1.0 diff --git a/Source/SwiftyCamViewController.swift b/Source/SwiftyCamViewController.swift index 0680e76..c43a283 100644 --- a/Source/SwiftyCamViewController.swift +++ b/Source/SwiftyCamViewController.swift @@ -356,7 +356,7 @@ open class SwiftyCamViewController: UIViewController { layer.videoOrientation = .portrait } - previewLayer.frame = self.view.bounds + previewLayer.frame = self.getPreviewViewParentView().bounds }