gpt4 book ai didi

ios - 使CAReplicator水平显示

转载 作者:行者123 更新时间:2023-11-29 05:23:03 24 4
gpt4 key购买 nike

我想知道是否有一种方法可以使用 CAReplicator,在我的用例中,它会创建两个堆叠在一起的相机预览,然后水平渲染这两个。我的 View Controller :

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)

关于ios - 使CAReplicator水平显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58469899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com