防止控制器对象层在解散模态控制器时消失

时间:2019-11-12 22:45:34

标签: ios swift avfoundation

我有一个VideoHandler类,它处理捕获视频。它具有所有常见的select()AVCaptureSessionAVCaptureMovieFileOutput-处理捕获视频所需的所有类。我在此类中也有一个AVCaptureDevice,以便使用此类的视图控制器可以快速打开相机。

以下是课程的摘要:

AVCapturePreviewViewLayer

但是,我遇到了以下问题:我插入了“ videoPreviewLayer class VideoHandler: NSObject{ var captureSession = AVCaptureSession(){ didSet { videoPlayerLayer.session = captureSession } } var videoOutput = AVCaptureMovieFileOutput() var currentCaptureDevice = AVCaptureDevice.default(for: .video) var delegate: VideoHandlerDelegate? lazy var videoPlayerLayer: AVCaptureVideoPreviewLayer = { let vpl = AVCaptureVideoPreviewLayer(session: captureSession) vpl.videoGravity = AVLayerVideoGravity.resizeAspectFill return vpl }() ... } dismiss()into the layer hierarchy on a modal view controller, but when I call AVCapturePreviewViewLayer`在动画开始之前消失了。如果我在控制器本身中手动创建AVCapturePreviewViewLayer并在那里设置会话,则不会发生这种情况,但是启动它会花费很多时间。

以下是两个版本的代码的示例:

加载迅速,但在on the modal view controller, there's a visual bug where the上出现了可视错误:

dismiss()

需要很长时间,但在class ModalViewController: UIViewController { override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) videoHandler.startCaptureSession() var videoPlayerLayer = videoHandler.videoPlayerLayer videoPlayerLayer.frame = view.bounds view.layer.insertSublayer(videoPlayerLayer, at: 0) } } 上没有视觉错误:

dismiss()

我假设必须删除VideoHandler类,但我不确定100%发生这种情况-当我在第一个代码示例中打印class ModalViewController: UIViewController { var videoPlayerLayer = AVCaptureVideoPreviewLayer() override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) videoHandler.startCaptureSession() videoPlayerLayer.session = videoHandler.captureSession videoPlayerLayer.frame = view.bounds view.layer.insertSublayer(videoPlayerLayer, at: 0) } } 中的子层时,得到那里有一个AVCaptureVideoPreviewLayer,所以这不像是在那里删除了图层,但是我不知道要检查其他地方。确保viewWillDisappear在模式完全消失之前不会被删除的正确方法是什么?

0 个答案:

没有答案