如何在 iOS 应用程序中停止 captureOutput

How to stop captureOutput in iOS app

我找到了使用二维码的教程 - here it is。使用这种方法我可以从设备相机获取图像并在那里找到 QR 码。

问题是当我更改 ViewController 甚至停止 AVCaptureSession - 似乎我的相机仍在工作并且设备正在搜索 QR 码。那么我应该如何禁用 captureOutput 方法呢?

我下载了演示项目并正在做

 self.captureSession?.stopRunning()

对我来说很好用

完整代码

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {

        // Check if the metadataObjects array is not nil and it contains at least one object.
        if metadataObjects == nil || metadataObjects.count == 0 {
            qrCodeFrameView?.frame = CGRectZero
            messageLabel.text = "No barcode/QR code is detected"
            return
        }
                self.captureSession?.stopRunning()

我在这个函数的开头设置了一个断点,一旦找到第一个二维码就不再调用它了。