Added support for video gravity

This commit is contained in:
Andrew Walz 2017-05-29 21:11:01 -06:00
parent 151bed4623
commit 62eef1dce0
2 changed files with 38 additions and 4 deletions

View file

@ -17,9 +17,32 @@ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
import UIKit
import AVFoundation
/// A function to specifty the Preview Layer's videoGravity. Indicates how the video is displayed within a player layers bounds rect.
public enum SwiftyCamVideoGravity {
/**
- Specifies that the video should be stretched to fill the layers bounds
- Corrsponds to `AVLayerVideoGravityResize`
*/
case resize
/**
- Specifies that the player should preserve the videos aspect ratio and fit the video within the layers bounds.
- Corresponds to `AVLayerVideoGravityResizeAspect`
*/
case resizeAspect
/**
- Specifies that the player should preserve the videos aspect ratio and fill the layers bounds.
- Correponds to `AVLayerVideoGravityResizeAspectFill`
*/
case resizeAspectFill
}
class PreviewView: UIView {
override init(frame: CGRect) {
private var gravity: SwiftyCamVideoGravity = .resizeAspect
init(frame: CGRect, videoGravity: SwiftyCamVideoGravity) {
gravity = videoGravity
super.init(frame: frame)
self.backgroundColor = UIColor.black
}
@ -29,7 +52,16 @@ class PreviewView: UIView {
}
var videoPreviewLayer: AVCaptureVideoPreviewLayer {
return layer as! AVCaptureVideoPreviewLayer
let previewlayer = layer as! AVCaptureVideoPreviewLayer
switch gravity {
case .resize:
previewlayer.videoGravity = AVLayerVideoGravityResize
case .resizeAspect:
previewlayer.videoGravity = AVLayerVideoGravityResizeAspect
case .resizeAspectFill:
previewlayer.videoGravity = AVLayerVideoGravityResizeAspectFill
}
return previewlayer
}
var session: AVCaptureSession? {

View file

@ -150,6 +150,8 @@ open class SwiftyCamViewController: UIViewController {
/// Sets whether or not View Controller supports auto rotation
public var allowAutoRotate = false
public var videoGravity : SwiftyCamVideoGravity = .resizeAspect
// MARK: Public Get-only Variable Declarations
@ -170,7 +172,7 @@ open class SwiftyCamViewController: UIViewController {
/// Current Capture Session
fileprivate let session = AVCaptureSession()
public let session = AVCaptureSession()
/// Serial queue used for setting up session
@ -242,7 +244,7 @@ open class SwiftyCamViewController: UIViewController {
override open func viewDidLoad() {
super.viewDidLoad()
view = PreviewView(frame: view.frame)
view = PreviewView(frame: view.frame, videoGravity: videoGravity)
previewLayer = view as! PreviewView!
// Add Gesture Recognizers