我想在单个视图上同时显示前面和后面相机视图(在同一单个UIView视图控制器中的2个单独的视图)。现在只显示后视图。我尝试了几件事,但我没有找到解决方案。我尝试创建了2个不同的捕获会话,但这并不起,我不知道它是否是NESECARY。

import UIKit;import AVFoundation

class ViewController: UIViewController,AVCapturePhotoCaptureDelegate {
    var captureSession: AVCaptureSession!
    var stillImageOutput: AVCapturePhotoOutput!
    var videoPreviewLayer: AVCaptureVideoPreviewLayer!
    
    
    
    
    var frontCameraView = UIView()
    
    
    var backCameraView = UIView()
    
    
    var heightNum = 30
    var brie = 0.1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        [frontCameraView,backCameraView].forEach{
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)
            
            
            $0.backgroundColor = UIColor(red: 0.1, green: 0.1, blue: CGFloat(brie), alpha: 1)
            heightNum += 120
            brie += 0.1
            $0.frame = CGRect(x: 100, y: heightNum, width: 100, height: 100)
            heightNum += 120
        }
        
        

        
        
        
        
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        // Setup your camera here...
        captureSession = AVCaptureSession()
        captureSession.sessionPreset = .medium
        
        
        
        guard let backCamera = AVCaptureDevice.default( AVCaptureDevice.DeviceType.builtInWideAngleCamera, for: AVMediaType.video, position: .back)
        //.default(for: AVMediaType.video)
        else {
            print("Unable to access back camera!")
            return
        }
        
        guard let frontCamera = AVCaptureDevice.default( AVCaptureDevice.DeviceType.builtInWideAngleCamera, for: AVMediaType.video, position: .front)
        //.default(for: AVMediaType.video)
        else {
            print("Unable to access back camera!")
            return
        }
        
        
        do {
            let input = try AVCaptureDeviceInput(device: backCamera)
            let input2 = try AVCaptureDeviceInput(device: frontCamera)
            //Step 9
            
            stillImageOutput = AVCapturePhotoOutput()
            
            if captureSession.canAddInput(input) && captureSession.canAddOutput(stillImageOutput) {
                captureSession.addInput(input)
                captureSession.addOutput(stillImageOutput)
                setupLivePreview()
            }
            if captureSession.canAddInput(input2) && captureSession.canAddOutput(stillImageOutput) {
                captureSession.addInput(input2)
                captureSession.addOutput(stillImageOutput)
                setupLivePreview()
            }
        }
        
        catch let error  {
            print("Error Unable to initialize back camera:  \(error.localizedDescription)")
        }
        
        
        
        
        
    }
    
    func setupLivePreview() {
        
        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        
        videoPreviewLayer.videoGravity = .resizeAspect
        videoPreviewLayer.connection?.videoOrientation = .portrait
        frontCameraView.layer.addSublayer(videoPreviewLayer)
        backCameraView.layer.addSublayer(videoPreviewLayer)
        
        //Step12
        
        DispatchQueue.global(qos: .userInitiated).async { //[weak self] in
            self.captureSession.startRunning()
            //Step 13
            DispatchQueue.main.async {
                self.videoPreviewLayer.frame = self.frontCameraView.bounds
            }
        }
    }
    
    
    
    
}

你好 @timswift.,感谢您分享您的代码和问题。我不知道是否有可能与您想要做的事情,但我确实从Apple的Avfoundation文档中找到了这个示例代码: avmulticampip:从多个摄像机捕获。也许示例代码可以提供可能的想法或解决方案。

最好,
吉娜