ios - 来自 NSDictionary 的 EXC_BAD_ACCESS

标签 ios objective-c cocoa-touch nsdictionary nsnumber

NSDictionary *orientation = [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithBool:YES], CIDetectorEyeBlink,
   [NSNumber numberWithInt:imgOrientation], CIDetectorImageOrientation, nil];

我的应用程序一直在这一行崩溃,给出可怕的 EXC_BAD_ACCESS (code=1, address=0x0)。我浏览了 NSDictionary 中名为“orientation”的所有内容,它们都是 objective-c 对象。有谁知道为什么会这样?任何事情都有帮助,因为我完全迷路了。这是该行所在的整个方法:

-(void)scanFace{

    UIImageOrientation imgOrientation = imgView.image.imageOrientation;
    orientation = [NSDictionary dictionaryWithObjectsAndKeys:
      [NSNumber numberWithBool:YES], CIDetectorEyeBlink, 
   [NSNumber numberWithInt:imgOrientation], CIDetectorImageOrientation, nil];

    CIImage *newImage = [CIImage imageWithCGImage:imgView.image.CGImage
                                       options:orientation];

    CIContext *context = [CIContext contextWithOptions:nil];
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace
        context:context options:[NSDictionary
      dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]];

    NSArray *features = [detector featuresInImage:newImage options:orientation];

    if (features.count == 0) {
        NSLog(@"NO FACE %i", features.count);
    } else {
        NSLog(@"FACE FOUND %i", features.count);
    }

    for (CIFaceFeature *feature in features) {

        if (feature.leftEyeClosed == YES) {
            leftLab.text = [NSMutableString stringWithFormat:@"Left Eye Closed"];
        } else {
            leftLab.text = [NSMutableString stringWithFormat:@"Left Eye Open"];
        }
        if (feature.rightEyeClosed == YES) {
            rightLab.text = [NSMutableString stringWithFormat:@"Right Eye Closed"];
        } else {
            rightLab.text = [NSMutableString stringWithFormat:@"Right Eye Open"];
        }

    }
}

这是堆栈跟踪:

0   FaceDetection                       0x000551a7 -[ViewController scanFace] + 66
1   FaceDetection                       0x000550cb __26-[ViewController takePic:]_block_invoke_2 + 282
2   libdispatch.dylib                   0x3c6ac793 <redacted> + 10
3   libdispatch.dylib                   0x3c6ac5db <redacted> + 22
4   libdispatch.dylib                   0x3c6afe45 _dispatch_main_queue_callback_4CF + 228
5   CoreFoundation                      0x3456b1b1 <redacted> + 1288
6   CoreFoundation                      0x344de23d CFRunLoopRunSpecific + 356
7   CoreFoundation                      0x344de0c9 CFRunLoopRunInMode + 104
8   GraphicsServices                    0x380bd33b GSEventRunModal + 74
9   UIKit                               0x363fa2b9 UIApplicationMain + 1120
10  FaceDetection                       0x00055e41 main + 116
11  libdyld.dylib                       0x3c6bfb20 <redacted> + 0

最佳答案

请注意 CIDetectorEyeBlink 出现在 iOS 7 中!

如果您尝试在旧版本中使用此 key ,您将获得 EXC_BAD_ACCESS,因为它未定义。

从 iOS7 开始,这段代码不会有问题。

关于ios - 来自 NSDictionary 的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22440045/

相关文章:

ios - 如何在 Facebook iOS SDK 中获取 Facebook 用户的完整好友列表

ios - 通知 UITableView dataSource 的变化

iphone - 子类化 UITableViewCell 后标签将不会显示文本

ios - __NSCFString timeIntervalSinceReferenceDate] : unrecognized selector sent to instance

ios - 我有一个分组的 UITableView,我需要让它看起来像一个普通的 UITableView

ios - 核心数据一对一关系返回 NULL

objective-c - Core Data 中的地址簿联系人

iphone - CGImageDestinationCreateWithData 没有创建 CGImageDestinationRef 对象

ios - 拍照时应用程序崩溃且没有错误

ios - NSProgress fractionCompleted 键值观察方法未调用