AVCaptureSession和白色屏幕

时间:2018-02-09 05:28:12

标签: ios11 swift4 avcapturesession

我的布局有两个视图见图: pic 1

View1是全屏,view2以红色边框框起是view1的子图层。两个视图都是透明的,除了摄像头广播之外什么都不做代码是:

import UIKit
import Foundation
import AVFoundation

class CameraView: UIView {

    override class var layerClass: AnyClass {
        get {
            return AVCaptureVideoPreviewLayer.self
        }
    }

    override var layer: AVCaptureVideoPreviewLayer {
        get {
            return super.layer as! AVCaptureVideoPreviewLayer
        }
    }
}

class SceneViewController: ARSceneViewController, 
AVCaptureMetadataOutputObjectsDelegate {

    var testFrameView: UIView?
    var cameraView: CameraView!

override func loadView() {
    super.loadView()
    cameraView = CameraView()
    testFrameView = cameraView
}

override func viewDidLoad() {
    super.viewDidLoad()

    if let testFrameView = testFrameView {
        testFrameView.layer.borderColor = UIColor.red.cgColor
        testFrameView.layer.borderWidth = 2
        testFrameView.layer.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
        view.addSubview(testFrameView)
        view.bringSubview(toFront: testFrameView)
        }
    }    
}

testFrameView是我的view2。现在我想让我的testFrameView捕获一些东西。这就是创建CameraView类的原因。我添加这样的输入:

import UIKit
import Foundation
import AVFoundation

class CameraView: UIView {

    override class var layerClass: AnyClass {
        get {
            return AVCaptureVideoPreviewLayer.self
        }
    }

    override var layer: AVCaptureVideoPreviewLayer {
        get {
            return super.layer as! AVCaptureVideoPreviewLayer
        }
    }
}

class SceneViewController: ARSceneViewController, 
AVCaptureMetadataOutputObjectsDelegate {

    var testFrameView: UIView?
    var cameraView: CameraView!

    /////////////////   NEW   /////////////////
    let session = AVCaptureSession()    
    let sessionQueue = DispatchQueue(label: AVCaptureSession.self.description(), attributes: [], target: nil) 
    /////////////////   NEW   /////////////////   

override func loadView() {
    super.loadView()
    cameraView = CameraView()
    testFrameView = cameraView
}

override func viewDidLoad() {
    super.viewDidLoad()

    if let testFrameView = testFrameView {
        testFrameView.layer.borderColor = UIColor.red.cgColor
        testFrameView.layer.borderWidth = 2
        testFrameView.layer.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
        view.addSubview(testFrameView)
        view.bringSubview(toFront: testFrameView)
        }
    }   

    /////////////////   NEW   /////////////////
    session.beginConfiguration()

    let videoDevice = AVCaptureDevice.default(for: .video)

    if (videoDevice != nil) {
        let videoDeviceInput = try? AVCaptureDeviceInput(device: videoDevice!)

        if (videoDeviceInput != nil) {
            if (session.canAddInput(videoDeviceInput!)) {
                session.addInput(videoDeviceInput!)
            }
        }
    }

    session.commitConfiguration()

    cameraView.layer.session = session 
    /////////////////   NEW   /////////////////
}

现在view2(又名testFrameView或CameraView)没问题,但是view1被白屏阻挡了,如下所示: pic 2

问题是:当我使用view2运行捕获会话时,为什么view1被阻止。如何避免呢?

0 个答案:

没有答案