我正在关注Pro iOS 5增强现实版中的面部识别应用示例。我甚至下载了源代码......我从那里运行它,问题仍然存在于他的代码中。这是问题:它在一个数组的赋值上崩溃,该数组为正在检测面的CIDetector获取CGImage的featuresInImage。从日志记录......似乎这个方法被多次调用...我正在使用cocos2d_chipmunk所以我使用的是CSScene。请注意,此次崩溃是EXC_BAD_ACCESS (code=1, address=0x4499923c)
请帮忙吗?
- (void)facialRecognitionRequest:(UIImage *)image {
//NSLog(@"Image is: %f by %f", image.size.width, image.size.height);
if (!isProcessingRequest) {
isProcessingRequest = YES;
//NSLog(@"Detecting Faces");
NSArray* arr = [detector featuresInImage:[CIImage imageWithCGImage:[image CGImage]]]; // CRASHES HERE
if ([arr count] > 0) {
//NSLog(@"Faces found.");
for (int i = 0; i < 1; i++) { //< [arr count]; i++) {
CIFaceFeature *feature = [arr objectAtIndex:i];
double xPosition = (feature.leftEyePosition.x + feature.rightEyePosition.x+feature.mouthPosition.x)/(3*image.size.width) ;
double yPosition = (feature.leftEyePosition.y + feature.rightEyePosition.y+feature.mouthPosition.y)/(3*image.size.height);
double dist = sqrt(pow((feature.leftEyePosition.x - feature.rightEyePosition.x),2)+pow((feature.leftEyePosition.y - feature.rightEyePosition.y),2))/image.size.width;
yPosition += dist;
CGSize size = [[CCDirector sharedDirector] winSize];
pumpkin.opacity = 255;
pumpkin.scale = 5*(size.width*dist)/256.0;
//int randomPumpkin = ((arc4random() % 10) + 5);
[pumpkin setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"pumpkin%d.png", pumpkin_count + 4]]];
CCMoveTo *moveAction = [CCMoveTo actionWithDuration:0 position:ccp((size.width * (xPosition)), (size.height * ((yPosition))))];
[pumpkin runAction:moveAction];
}
} else {
pumpkin.opacity = 0;
}
}
isProcessingRequest = NO;
}
分配CIDetector:
- (id)init {
if (self = [super init]) {
// ....... other stuff here
NSDictionary *detectorOptions = [NSDictionary dictionaryWithObjectsAndKeys:CIDetectorAccuracyLow, CIDetectorAccuracy, nil];
self.detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:detectorOptions]; // CIDetector instance named detector is my property
}
return self;
}
我试过了: CGImage * theCGImage = [image CGImage]; NSLog(@“theCGImage:%@”,theCGImage);
CIImage *theCIImage = [CIImage imageWithCGImage:theCGImage];
NSLog(@"theCIImage: %@", theCIImage);
NSArray* arr = [detector featuresInImage:theCIImage];
NSLog(@"arr: %@", arr);
结果如下:
2012-04-15 19:08:25.136 Ch8[981:609f] tmpCGImage: <CGImage 0x1f689c00>
2012-04-15 19:08:25.143 Ch8[981:609f] tmpCIImage: <CIImage: 0x1f687970 extent [0 0 480 360]>
2012-04-15 19:08:25.282 Ch8[981:609f] arr: (
"<CIFaceFeatureInternal: 0x1f58e080>"
)
我也试过启用NSZombies
,但仍然没有运气......任何想法?
答案 0 :(得分:0)
回答评论(不是整个问题,仅作为格式的答案):
“我该怎么做呢?我只是写if语句,看看它们是否为非零然后记录它?”
而不是:
NSArray* arr = [detector featuresInImage:[CIImage imageWithCGImage:[image CGImage]]];
将其分为三个陈述:
CGImage *theCGImage = [image CGImage];
NSLog(@"theCGImage: %@", theCGImage);
CIImage *theCIImage = [CIImage imageWithCGImage:theCGImage];
NSLog(@"theCIImage: %@", theCIImage);
NSArray* arr = [detector featuresInImage:theCIImage];
NSLog(@"arr: %@", arr);
这样就可以找到违规的陈述。这是一种通用的调试技术,在任何情况下都不是编写代码的坏方法。
NSLog
语句不是必需的,是第一个语句的断点,然后单步执行。
对于因过早发布而导致的崩溃,请使用NSZombies
。它可以在Xcode中的“编辑方案,选项卡:”诊断“下启用,确保在设备上运行时将其关闭。
答案 1 :(得分:0)
检查是否添加了异常断点。 以我为例,在功能函数调用中引发了异常,而试图捕获该异常的断点导致崩溃。通过从“断点”列表中删除“异常断点”,崩溃消失了。