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

How to stop capture session when it is inside the Collection View Cell in iOS , Swift

我在 iOS 中使用 (AVFoundation) AVCapture Session 实现了 QR Reader。在我的 ParentViewController 中,我实现了一个 CollectionView。在 collection view cell 中我已经实现了 QR Code Reader 并且我在 cell 中开始捕获会话开始 运行 .它工作正常并读取元数据输出。当调用元数据输出委托时,我停止会话。但是,如果我离开 ParentView Controller,Capture Session 仍然是 运行,当我导航到另一个 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)])
        }

但没有成功,失败了。谁能帮忙解决这个问题。

您正在创建 QRReaderCell 的新实例。

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

无需创建新实例,只需访问包含 QRReader 的当前单元格即可。

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

并确保您的 viewWillDisappear 接到电话。如果没有,您可以在导航到另一个 viewController 之前停止捕获会话。 只需在推送或继续到另一个 viewController.

之前停止捕获会话

似乎更有可能存在保留周期,但如果不查看您的单元配置代码则很难判断。

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

如果您 post 单元格 class 和控制器 class 代码可能会有所帮助。