AVcaptureMetadataOutput 在 ios10 中不起作用

AVcaptureMetadataOutput does not work in ios10

我将 AVCaptureMetadataOutputObjectsDelegate 用于条码扫描仪,效果非常好。但是因为 iOS 10 它不再工作了。当我用它的按钮打开 barcodescanner 时,我总是收到 EXC_BAD_ACCESS 错误。当我向会话添加输出时发生错误。

有人可以帮帮我吗?我真的什么都试过了,这让我发疯。

这是我目前正在使用的线路。

    func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
    // Get the first object from the metadataObjects array.
    if let barcodeData = metadataObjects.first {
        // Turn it into machine readable code
        let barcodeReadable = barcodeData as? AVMetadataMachineReadableCodeObject;
        if let readableCode = barcodeReadable {
            // Send the barcode as a string to barcodeDetected()
            barcodeDetected(readableCode.stringValue);
        }
        // Vibrate the device to give the user some feedback.
        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
        // Avoid a very buzzy device.
        session.stopRunning()
    }
}

这是我的 Objective-C,它在 iOS 10 下运行良好。IIRC 我想如果变量只是方法的本地变量,我会遇到麻烦。确保您所有的变量都是属性。

- (void)initialiseVideoSession {

    _session = [[AVCaptureSession alloc] init];
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    NSError *error = nil;

    _input = [AVCaptureDeviceInput deviceInputWithDevice:_device error:&error];

    if (_input) {

        [_session addInput:_input];

        _output = [[AVCaptureMetadataOutput alloc] init];
        [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        [_session addOutput:_output];

        _output.metadataObjectTypes = [_output availableMetadataObjectTypes];

        _prevLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
        _prevLayer.frame = self.view.bounds;
        _prevLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        [self.view.layer addSublayer:_prevLayer];

        [_session startRunning];

    } else {
        // Error
    }
}