使CAReplicator水平显示

问题描述 投票:0回答:1

我想知道是否有办法采用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
                }
            }
        }
    }

}

现在,这将显示两个垂直堆叠的摄像机预览,我想并排,这样,当我侧向翻转设备时,它们会填满屏幕(一个预览在左侧,另一个在右侧)。 >

我想知道是否有办法采用CAReplicator,在我的用例中,它创建了两个彼此叠放的摄像机预览,而是水平渲染这两个摄像机。我的...

ios swift avfoundation careplicatorlayer
1个回答
0
投票

就像换行一样简单

© www.soinside.com 2019 - 2024. All rights reserved.