У меня есть AVCaptureVideoPreviewLayer, и я хотел бы добавить его в UIView (например, @IB) из моей раскадровки. Как мне go это сделать?
Это код для моего AVCaptureVideoPreviewLayer:
import UIKit
import AVFoundation
class ViewController: UIViewController, PreviewDelegate {
var cameraPreviewLayer: AVCaptureVideoPreviewLayer!
@IBOutlet weak var topBorder: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Functions for capturing image
setupCaptureSession()
setupDevice()
setupInputOutput()
setupPreviewLayer()
startRunningCaptureSession()
super.viewDidLoad();
}
func setupPreviewLayer() {
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspect
cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
cameraPreviewLayer?.frame = self.view.frame
self.view.layer.insertSublayer(cameraPreviewLayer!, at: 0)
cameraPreviewLayer?.anchorPoint = CGPoint(x: 0, y: 0)
cameraPreviewLayer?.position = CGPoint(x: 0, y: topBorder.frame.height)
}
}