使CAReplicator水平显示
Making CAReplicator display horizontally
我想知道是否有一种方法可以使用 CAReplicator,在我的用例中,它创建了两个堆叠在一起的相机预览,而不是水平渲染这两个。我的 ViewController:
import UIKit
import AVFoundation
class ViewController: UIViewController {
let captureSession = AVCaptureSession()
var previewLayer:AVCaptureVideoPreviewLayer!
var replicatorLayer = CAReplicatorLayer()
var captureDevice:AVCaptureDevice!
override func viewDidLoad() {
super.viewDidLoad()
prepareCamera()
}
func prepareCamera(){
captureSession.sessionPreset = AVCaptureSession.Preset.photo
let availableDevices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back).devices
captureDevice = availableDevices.first
beginSession()
}
func beginSession(){
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice)
captureSession.addInput(captureDeviceInput)
} catch {
print(error.localizedDescription)
}
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
self.previewLayer = previewLayer
self.previewLayer.videoGravity = .resizeAspectFill
self.previewLayer.frame = self.view.bounds//CGRect(x: 0.0, y: 0.0, width: view.bounds.size.width, height: view.bounds.size.height / 2)
let replicatorInstances = 2
replicatorLayer.frame = CGRect(x: 0, y: 0, width: view.bounds.size.width, height: CGFloat(view.bounds.size.height / CGFloat(replicatorInstances)))
replicatorLayer.instanceCount = replicatorInstances
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(0.0, view.bounds.size.height / CGFloat(replicatorInstances), 0.0)
replicatorLayer.addSublayer(previewLayer)
self.view.layer.addSublayer(replicatorLayer)
captureSession.startRunning()
let dataOutput = AVCaptureVideoDataOutput()
dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString):NSNumber(value:kCVPixelFormatType_32BGRA)] as [String : Any]
dataOutput.alwaysDiscardsLateVideoFrames = true
if captureSession.canAddOutput(dataOutput) {
captureSession.addOutput(dataOutput)
}
captureSession.commitConfiguration()
let queue = DispatchQueue(label: "clockworksciencce")
dataOutput.setSampleBufferDelegate(self as? AVCaptureVideoDataOutputSampleBufferDelegate, queue: queue)
}
private func updatePreviewLayer(layer: AVCaptureConnection, orientation: AVCaptureVideoOrientation) {
layer.videoOrientation = orientation
previewLayer.frame = self.view.bounds
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if let connection = self.previewLayer?.connection {
let currentDevice: UIDevice = UIDevice.current
let orientation: UIDeviceOrientation = currentDevice.orientation
let previewLayerConnection : AVCaptureConnection = connection
if previewLayerConnection.isVideoOrientationSupported {
switch (orientation) {
case .portrait: updatePreviewLayer(layer: previewLayerConnection, orientation: .portrait)
break
case .landscapeRight: updatePreviewLayer(layer: previewLayerConnection, orientation: .landscapeLeft)
break
case .landscapeLeft: updatePreviewLayer(layer: previewLayerConnection, orientation: .landscapeRight)
break
case .portraitUpsideDown: updatePreviewLayer(layer: previewLayerConnection, orientation: .portraitUpsideDown)
break
default: updatePreviewLayer(layer: previewLayerConnection, orientation: .portrait)
break
}
}
}
}
}
现在这显示了两个垂直堆叠的相机预览,我想并排放置,这样,当我将设备横向转动时,它们会填满屏幕(一个预览在左边,另一个在右边).
换线就这么简单
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(0.0, view.bounds.size.height / CGFloat(replicatorInstances), 0.0)
至
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(view.bounds.size.height / CGFloat(replicatorInstances), 0.0, 0.0)
我想知道是否有一种方法可以使用 CAReplicator,在我的用例中,它创建了两个堆叠在一起的相机预览,而不是水平渲染这两个。我的 ViewController:
import UIKit
import AVFoundation
class ViewController: UIViewController {
let captureSession = AVCaptureSession()
var previewLayer:AVCaptureVideoPreviewLayer!
var replicatorLayer = CAReplicatorLayer()
var captureDevice:AVCaptureDevice!
override func viewDidLoad() {
super.viewDidLoad()
prepareCamera()
}
func prepareCamera(){
captureSession.sessionPreset = AVCaptureSession.Preset.photo
let availableDevices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back).devices
captureDevice = availableDevices.first
beginSession()
}
func beginSession(){
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice)
captureSession.addInput(captureDeviceInput)
} catch {
print(error.localizedDescription)
}
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
self.previewLayer = previewLayer
self.previewLayer.videoGravity = .resizeAspectFill
self.previewLayer.frame = self.view.bounds//CGRect(x: 0.0, y: 0.0, width: view.bounds.size.width, height: view.bounds.size.height / 2)
let replicatorInstances = 2
replicatorLayer.frame = CGRect(x: 0, y: 0, width: view.bounds.size.width, height: CGFloat(view.bounds.size.height / CGFloat(replicatorInstances)))
replicatorLayer.instanceCount = replicatorInstances
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(0.0, view.bounds.size.height / CGFloat(replicatorInstances), 0.0)
replicatorLayer.addSublayer(previewLayer)
self.view.layer.addSublayer(replicatorLayer)
captureSession.startRunning()
let dataOutput = AVCaptureVideoDataOutput()
dataOutput.videoSettings = [(kCVPixelBufferPixelFormatTypeKey as NSString):NSNumber(value:kCVPixelFormatType_32BGRA)] as [String : Any]
dataOutput.alwaysDiscardsLateVideoFrames = true
if captureSession.canAddOutput(dataOutput) {
captureSession.addOutput(dataOutput)
}
captureSession.commitConfiguration()
let queue = DispatchQueue(label: "clockworksciencce")
dataOutput.setSampleBufferDelegate(self as? AVCaptureVideoDataOutputSampleBufferDelegate, queue: queue)
}
private func updatePreviewLayer(layer: AVCaptureConnection, orientation: AVCaptureVideoOrientation) {
layer.videoOrientation = orientation
previewLayer.frame = self.view.bounds
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if let connection = self.previewLayer?.connection {
let currentDevice: UIDevice = UIDevice.current
let orientation: UIDeviceOrientation = currentDevice.orientation
let previewLayerConnection : AVCaptureConnection = connection
if previewLayerConnection.isVideoOrientationSupported {
switch (orientation) {
case .portrait: updatePreviewLayer(layer: previewLayerConnection, orientation: .portrait)
break
case .landscapeRight: updatePreviewLayer(layer: previewLayerConnection, orientation: .landscapeLeft)
break
case .landscapeLeft: updatePreviewLayer(layer: previewLayerConnection, orientation: .landscapeRight)
break
case .portraitUpsideDown: updatePreviewLayer(layer: previewLayerConnection, orientation: .portraitUpsideDown)
break
default: updatePreviewLayer(layer: previewLayerConnection, orientation: .portrait)
break
}
}
}
}
}
现在这显示了两个垂直堆叠的相机预览,我想并排放置,这样,当我将设备横向转动时,它们会填满屏幕(一个预览在左边,另一个在右边).
换线就这么简单
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(0.0, view.bounds.size.height / CGFloat(replicatorInstances), 0.0)
至
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(view.bounds.size.height / CGFloat(replicatorInstances), 0.0, 0.0)