ios - ios 7 的 QRScanner 代码经常崩溃
问题描述
我在目标 c 中集成了一个 QRCode 扫描仪,主要问题是它经常在较小的设备上崩溃,我在 iPhone 新系列上检查了同样的情况,虽然我只崩溃了一次,但代码工作正常。有人可以建议以下代码有什么问题吗?它与线程或设备ios7的体系结构有关吗?
解决方案
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
// zzzzzzz
// dispatch_semaphore_t s = dispatch_semaphore_create ( 0 );
// __block BOOL f = NO;
NSString *detectionString = nil;
NSArray *barCodeTypes = @[AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode39Mod43Code,
AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeQRCode, AVMetadataObjectTypeAztecCode];
for (AVMetadataObject *metadata in metadataObjects) {
for (NSString *type in barCodeTypes) {
if ([metadata.type isEqualToString:type])
{
detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
// xxxxxxxxxxxxx inside code
// this happens on main thread
// at some undetermined time in the future
// maybe change to sync or figure out how
// this must be called relative to outside
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:detectionString preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//button click event
// zzzzzzzzzz
// f = YES;
// dispatch_semaphore_signal( s );
[self stopReading];
[self sendResult:detectionString];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//button click event
// zzzzzzzzzz
// f = NO;
// dispatch_semaphore_signal( s );
}];
[alert addAction:cancel];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
});
break;
}
}
if (detectionString != nil)
{
// xxxxxxxxxxxx outside code
// this happens on the main thread
// it can happen before, after or while the alert is
// displayed in the inside code
// I think there is a dependency between outside and
// inside code ...
// Everything is done on the main thread.
// zzzzzzzzzzzzzz
// dispatch_semaphore_wait ( s, DISPATCH_TIME_FOREVER );
// if ( f )
// {
// [self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
// [self performSelectorOnMainThread:@selector(sendResult:) withObject:detectionString waitUntilDone:YES];
// }
break;
}
}
}
您需要弄清楚内部代码和外部代码之间的关系是什么。如果根本存在关系,例如在调用外部之前必须先完成内部,那么这就是你的问题。
我不知道扫描仪,但只是注意到这可能会造成麻烦,因为我认为在你打电话给外面之前必须先在里面完成。
编辑
如果我的预感是正确的,那么请查看带有 zzzzz 的代码。这是我的第二次编辑。第一个只是做了 xxxxx 来解释我认为需要什么。我添加的 zzzzz 将同步内部和外部,但这实际上只是一个猜测 - 也许这会破坏代码。但至少你可以快速测试一下。
** 编辑 - 再次 **
好的,我又改了。以前我使用了一个信号量,因为它可以巧妙地融入您的代码结构,但它有点过分,我已经对其进行了一些更改以摆脱 sem 并简化。无论如何,让我知道进展如何。
推荐阅读
- java - 从弹簧数据 jpa 返回自定义复杂对象的选项
- go - 从 github 获取 go 包时出错
- performance - 对于 jmeter 中的应用程序的压力测试,我们应该增加每小时的交易量还是增加用户数?
- javascript - 如何让这个函数同步运行?
- python-3.x - python pandas dataframe.plot()
- javascript - 在调用函数之前对 React 数组进行排序
- google-apps-script - 无法使用 clasp push 命令推送文件
- elm - 如何导出控制台日志 win Elm?
- c++ - 类内定义的C++17演绎指南在类内无效,但在类外有用
- excel - 比较表 1 的 B 列与表 2 的 B 列中的值;在工作表 2 中附加不匹配的值