如何在iOS,Swift中的Collection View Cell内停止捕获会话

问题描述 投票:0回答:2

我在iOS中使用(AVFoundation)AVCapture Session实现了一个QR阅读器。在我的ParentViewController中,我实现了一个CollectionView。在集合视图单元格中,我已经实现了QR Code Reader,我开始在单元格内运行捕获会话。它工作正常并读取元数据输出。当元数据输出委托调用时,我停止会话。但是如果我离开ParentView控制器,捕获会话仍在运行,当我导航到另一个ViewController时,它会从后台捕获会话。为什么这样以及如何在导航到另一个视图控制器时停止捕获会话。

这是层次结构,

ParentViewController --->(inside)CollectionView --->(inside) CollectionViewCell --->(inside) QR Reader with capture session Strat.

怎么了,

ParentViewController ----> (navigate to another controller) Capture Session Still Activated and reads qr codes from background 

我想要的是,

ParentViewController ---->(navigate to another controller) Shouldn't capture anything.

我试着在ParentViewController里面,在viewWillDisappear里面

    override open func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)

         let qrreaderCell = QRReaderCell()
         qrreaderCell.captureSession.stopRunning()
         ColletionView.reloadItems(at: [IndexPath(row: 0, section: 0)])
        }

但没有工作,失败了。有人能帮忙吗。

ios swift avfoundation avcapturesession
2个回答
2
投票

您正在创建QRReaderCell的新实例。

let qrreaderCell = QRReaderCell()
qrreaderCell.captureSession.stopRunning()

而不是创建新实例,只需访问包含QRReader的当前单元格。

let qrreaderCell = collectionView.cellForItem(at: IndexPath(row: 0, section: 0))
qrreaderCell.captureSession.stopRunning()

并确保你的viewWillDisappear被召唤。如果没有,您可以在导航到另一个viewController之前立即停止捕获会话。只需在推送或转到另一个viewController之前停止捕获会话。


0
投票

似乎更有可能存在保留周期,但如果没有看到您的单元配置代码则很难分辨。

要停止现有会话,您应该迭代集合视图并在任何已存在的单元格上停止会话。您可以在viewDidDisappear中执行此操作(您的方法创建了一个新单元格,无论如何在方法结束时都会释放)

如果您发布单元格类和控制器类代码,也许它可能会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.