mirror of
https://github.com/samsonjs/SwiftyCam.git
synced 2026-04-27 15:07:43 +00:00
Added support for video gravity
This commit is contained in:
parent
151bed4623
commit
62eef1dce0
2 changed files with 38 additions and 4 deletions
|
|
@ -17,9 +17,32 @@ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||||
import UIKit
|
import UIKit
|
||||||
import AVFoundation
|
import AVFoundation
|
||||||
|
|
||||||
|
/// A function to specifty the Preview Layer's videoGravity. Indicates how the video is displayed within a player layer’s bounds rect.
|
||||||
|
public enum SwiftyCamVideoGravity {
|
||||||
|
|
||||||
|
/**
|
||||||
|
- Specifies that the video should be stretched to fill the layer’s bounds
|
||||||
|
- Corrsponds to `AVLayerVideoGravityResize`
|
||||||
|
*/
|
||||||
|
case resize
|
||||||
|
/**
|
||||||
|
- Specifies that the player should preserve the video’s aspect ratio and fit the video within the layer’s bounds.
|
||||||
|
- Corresponds to `AVLayerVideoGravityResizeAspect`
|
||||||
|
*/
|
||||||
|
case resizeAspect
|
||||||
|
/**
|
||||||
|
- Specifies that the player should preserve the video’s aspect ratio and fill the layer’s bounds.
|
||||||
|
- Correponds to `AVLayerVideoGravityResizeAspectFill`
|
||||||
|
*/
|
||||||
|
case resizeAspectFill
|
||||||
|
}
|
||||||
|
|
||||||
class PreviewView: UIView {
|
class PreviewView: UIView {
|
||||||
|
|
||||||
override init(frame: CGRect) {
|
private var gravity: SwiftyCamVideoGravity = .resizeAspect
|
||||||
|
|
||||||
|
init(frame: CGRect, videoGravity: SwiftyCamVideoGravity) {
|
||||||
|
gravity = videoGravity
|
||||||
super.init(frame: frame)
|
super.init(frame: frame)
|
||||||
self.backgroundColor = UIColor.black
|
self.backgroundColor = UIColor.black
|
||||||
}
|
}
|
||||||
|
|
@ -29,7 +52,16 @@ class PreviewView: UIView {
|
||||||
}
|
}
|
||||||
|
|
||||||
var videoPreviewLayer: AVCaptureVideoPreviewLayer {
|
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? {
|
var session: AVCaptureSession? {
|
||||||
|
|
|
||||||
|
|
@ -151,6 +151,8 @@ open class SwiftyCamViewController: UIViewController {
|
||||||
|
|
||||||
public var allowAutoRotate = false
|
public var allowAutoRotate = false
|
||||||
|
|
||||||
|
public var videoGravity : SwiftyCamVideoGravity = .resizeAspect
|
||||||
|
|
||||||
|
|
||||||
// MARK: Public Get-only Variable Declarations
|
// MARK: Public Get-only Variable Declarations
|
||||||
|
|
||||||
|
|
@ -170,7 +172,7 @@ open class SwiftyCamViewController: UIViewController {
|
||||||
|
|
||||||
/// Current Capture Session
|
/// Current Capture Session
|
||||||
|
|
||||||
fileprivate let session = AVCaptureSession()
|
public let session = AVCaptureSession()
|
||||||
|
|
||||||
/// Serial queue used for setting up session
|
/// Serial queue used for setting up session
|
||||||
|
|
||||||
|
|
@ -242,7 +244,7 @@ open class SwiftyCamViewController: UIViewController {
|
||||||
|
|
||||||
override open func viewDidLoad() {
|
override open func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
view = PreviewView(frame: view.frame)
|
view = PreviewView(frame: view.frame, videoGravity: videoGravity)
|
||||||
previewLayer = view as! PreviewView!
|
previewLayer = view as! PreviewView!
|
||||||
|
|
||||||
// Add Gesture Recognizers
|
// Add Gesture Recognizers
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue