captureOutput:didOutputMetadataObjects:fromConnection:多次重命名。我需要将扫描值传递给服务器,以便多次调用服务器进行一次扫描
答案 0 :(得分:0)
这是同一个问题 Delegate method being invoked multiple times, even after returning to a view
正如Apple文档所说:"这种方法可能经常被调用,您的实现应该有效地防止捕获性能问题,包括丢弃的元数据对象"。
处理多次调用执行以下操作:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
id capturedData;
if ([metadataObjects count] > 0) {
// handle your captured data here
[self performSelectorOnMainThread:@selector(stopReading:) withObject:capturedData waitUntilDone:NO];
}
}
stopReading:方法看起来(假设您的_session是AVCaptureSession对象而_prevLayer是您之前使用过的AVCaptureVideoPreviewLayer):
-(void)stopReading:(id) data{
NSLog(@"stop reading");
[_session stopRunning];
_session = nil;
[_prevLayer removeFromSuperlayer];
// do what you want with captured data
[self.delegate didScanBarCodeWithContext:data];
}