我试图在预览层上放置一个叠加层,但是由于某些原因,未显示添加到预览层的UIImageView。

码:

let session = AVCaptureSession()
let sessionQueue = DispatchQueue(label: AVCaptureSession.self.description(), attributes: [], target: nil)
let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: 240, height: 80))

override func viewDidLoad() {
    super.viewDidLoad()

    imgView.image = UIImage(contentsOfFile: "angleArm.png")

    session.beginConfiguration()
    let videoDevice = AVCaptureDevice.default(.builtInDualCamera, for: AVMediaType.video, position: .back)
    if (videoDevice != nil) {
        let videoDeviceInput = try? AVCaptureDeviceInput(device: videoDevice!)
        if (videoDeviceInput != nil) {
            if (session.canAddInput(videoDeviceInput!)) {
                session.addInput(videoDeviceInput!)
            }
        }
    }

    session.commitConfiguration()

    let previewLayer = AVCaptureVideoPreviewLayer(session: session)
    previewLayer.frame = view.frame

    previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
    self.view.layer.addSublayer(previewLayer)

    let preView = UIView()
    preView.frame = self.view.frame
    preView.addSubview(imgView)
    self.view.addSubview(preView)
    self.view.bringSubviewToFront(preView)

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    sessionQueue.async {
        self.session.startRunning()
    }
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    sessionQueue.async {
        self.session.stopRunning()
    }
}


我已经阅读了一些文章,并尝试使用CALayer并将imageView添加到其内容中,到目前为止,我尝试创建另一个UIView,将imageView放入其中并将其添加到主视图中,但还是没有运气。

有人知道我在做什么错吗?提前致谢。

最佳答案

您应该检查并更改叠加视图的zPosition。我认为您应该将overlayView.layer.zPosition> Preview.layer.zPosition。

例如代码:
overlayView.layer.zPosition = Preview.layer.zPosition + 1

关于swift - Swift-叠加层未在AVCaptureVideoPreviewLayer上显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57241640/

10-12 14:31