程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在同一类中显示相机的前后视图大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在同一类中显示相机的前后视图?

开发过程中遇到在同一类中显示相机的前后视图的问题如何解决?下面主要结合日常开发的经验,给出你关于在同一类中显示相机的前后视图的解决方法建议,希望对你解决在同一类中显示相机的前后视图有所启发或帮助;

我想同时在单个视图上显示前置和后置摄像头视图(同一个 UIVIEw 视图控制器中的 2 个独立视图)。现在只显示后视图。我已经尝试了几件事,但我还没有找到解决方案。我尝试创建了 2 个不同的捕获会话,但是没有用,我不知道它是否是 nescary。

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
        }
        
        
        NSLayoutConstraint.activate([
            
            frontCameraview.topAnchor.constraint(equalTo: vIEw.safeAreaLayoutGuIDe.topAnchor),frontCameraview.leadingAnchor.constraint(equalTo: vIEw.leadingAnchor),frontCameraview.heightAnchor.constraint(equalTo: vIEw.heightAnchor,multiplIEr: 1/3),frontCameraview.wIDthAnchor.constraint(equalTo: vIEw.wIDthAnchor,multiplIEr: 1/2),backCameraview.topAnchor.constraint(equalTo: vIEw.safeAreaLayoutGuIDe.topAnchor),backCameraview.leadingAnchor.constraint(equalTo: frontCameraview.trailingAnchor),backCameraview.heightAnchor.constraint(equalTo: vIEw.heightAnchor,backCameraview.wIDthAnchor.constraint(equalTo: vIEw.wIDthAnchor,])
        
        
        
        
    }
    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,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
            }
        }
    }
    
    
    
    
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的在同一类中显示相机的前后视图全部内容,希望文章能够帮你解决在同一类中显示相机的前后视图所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: