Reputation: 27
I used the AVCaptureMetadataOutputObjectsDelegate
for a barcode scanner and it worked perfect. But since iOS 10 it does not working anymore. I always get a EXC_BAD_ACCESS error when I open the barcodescanner
with it´s button. The error occurs when I add an output to the session.
Can someone help me please? I really tried everything and it drives me crazy.
Upvotes: 0
Views: 716
Reputation: 1252
Here's my Objective-C, which is working fine under iOS 10. IIRC I think I had trouble if the vars were only local to the method. Ensure all your vars are properties.
- (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
}
}
Upvotes: 1
Reputation: 108
Here is my line which is works currently.
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()
}
}
Upvotes: 0