gpt4 book ai didi

iOS延迟我无法弄清楚使用AVFoundation扫描条形码

转载 作者:行者123 更新时间:2023-11-29 02:56:26 24 4
gpt4 key购买 nike

我有一个时间问题让我很困惑,因为我使用委托(delegate)将数据发送回 HomeView 进行处理并重定向到问题所在的 ScanResults 但即使我

[self performSegueWithIdentifier:@"ScanResultsSegue" sender:self];

直接到它我仍然看到一个巨大的延迟,直到 ScanResultsView 出现。这是 NSLog。最后一行显示延迟

2014-05-24 16:20:55.640 selfcheckin[2748:60b] StartReading Called
2014-05-24 16:21:03.519 selfcheckin[2748:3603] We have a QR -> performSelectorOnMainThread
2014-05-24 16:21:03.522 selfcheckin[2748:3603] IsMainThread: NO
2014-05-24 16:21:03.549 selfcheckin[2748:3603] Barcode: {"stop_id":"3","event_id":"1"}
2014-05-24 16:21:03.555 selfcheckin[2748:3603] didScan Delegate called!
2014-05-24 16:21:03.576 selfcheckin[2748:3603] Parsing Json Dictionary
2014-05-24 16:21:03.584 selfcheckin[2748:3603] Event_id: 1
2014-05-24 16:21:03.590 selfcheckin[2748:3603] Stop_id: 3
2014-05-24 16:21:03.603 selfcheckin[2748:3603] Account_id: 4
2014-05-24 16:21:04.782 selfcheckin[2748:60b] StopReading Called
2014-05-24 16:21:14.721 selfcheckin[2748:3603] Made it to Success!!!

转换到 ScanResultsPage 需要 10 秒.......我无法在我的代码中找到导致它的原因。需要那么久吗??相关部分

- (BOOL)startReading {
NSError *error;

AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
if (!input) {
NSLog(@"%@", [error localizedDescription]);
return NO;
}

_captureSession = [[AVCaptureSession alloc] init];
[_captureSession addInput:input];

AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
[_captureSession addOutput:captureMetadataOutput];

dispatch_queue_t dispatchQueue;
dispatchQueue = dispatch_queue_create("myQueue", NULL);
[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];

_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_videoPreviewLayer setFrame:_viewPreview.layer.bounds];
[_viewPreview.layer addSublayer:_videoPreviewLayer];

[_captureSession startRunning];
NSLog(@"%@",@"StartReading Called");
return YES;
}

-(void)stopReading{
[_captureSession stopRunning];
_captureSession = nil;
[_videoPreviewLayer removeFromSuperlayer];
NSLog(@"%@",@"StopReading Called");
}

-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
NSLog(@"We have a QR -> performSelectorOnMainThread");
NSLog(@"IsMainThread: %@", ([NSThread isMainThread]) ? @"YES" : @"NO");
[self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
_isReading = NO;
NSLog(@"Barcode: %@",[(AVMetadataMachineReadableCodeObject *)metadataObj stringValue]);
//NSLog(@"Now performSegueWithIdentifier");
//[self performSegueWithIdentifier:@"ScanResultsSegue" sender:self];
NSLog(@"didScan Delegate called!");
[self.scandelegate didScan:self barcodedata:[(AVMetadataMachineReadableCodeObject *)metadataObj stringValue]];
}
}
}

最佳答案

此类问题几乎总是由尝试与后台线程上的界面对话引起的。与接口(interface)的所有通信必须在主线程上。

总的来说,您的代码在线程方面非常不连贯。您可以从日志记录中看到这一点,它显示了两个不同的线程(主线程和后台线程)交错。另请注意,这是荒谬且危险的:

[self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
_isReading = NO;

您现在正在后台线程中访问 _isReading,因此线程之间可能会对此 ivar 产生争用。而且,你在说谎;您仍在阅读,因为stopReading尚未被调用(如日志记录所示):waitUntilDone为NO,因此代码立即继续,并且 stopReading 稍后在主线程上调用。一切都乱了套,杂乱无章。

您知道 captureOutput: 正在后台线程上调用,因此此时您需要开始非常非常小心。如果您没有理由处于后台线程,最安全的做法是立即进入主线程 (dispatch_async),以便该方法的其余部分在主线程上执行。如果您确实有理由进入后台线程,那么至少要确保您不会做线程不安全的事情,就像您现在所做的那样。

关于iOS延迟我无法弄清楚使用AVFoundation扫描条形码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23849465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com