Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom previewLayer parent view #223

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion DemoSwiftyCam/DemoSwiftyCam/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 10 additions & 6 deletions Source/SwiftyCamViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -352,7 +356,7 @@ open class SwiftyCamViewController: UIViewController {
layer.videoOrientation = .portrait
}

previewLayer.frame = self.view.bounds
previewLayer.frame = self.getPreviewViewParentView().bounds

}

Expand Down