1

我试图弄清楚如何从用户的相机胶卷中获取图像并在其上运行一些核心图像操作(检测面部)。我能够成功地将图像加载到 uiimageview 中,但由于某种原因,我无法使用该图像来使用核心图像检测人脸。有问题的数组是代码的最后一点,这个数组应该包含 CIFaceFeatures,但它一直是空的。我尝试过使用不同的图像,但仍然无法正常工作。有任何想法吗?

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *image =  [info valueForKey:@"UIImagePickerControllerOriginalImage"];
self.facePicture.image = image;

CIImage* image = [CIImage imageWithCGImage:self.facePicture.image.CGImage];

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

NSArray* features = [detector featuresInImage:image]; //***features array is empty
NSLog(@"array count is %i",features.count); //returns 0
//Code continues with face detection stuff

}
4

1 回答 1

1

尝试更改以下行

UIImage *image =  [info valueForKey:@"UIImagePickerControllerOriginalImage"];

至:

UIImage *image =  [info objectForKey:UIImagePickerControllerOriginalImage];

重要的部分是从 using@"UIImagePickerControllerOriginalImage"到 using的转变UIImagePickerControllerOriginalImage。其原因UIImagePickerControllerOriginalImage是声明如下:

extern NSString *const UIImagePickerControllerOriginalImage;

在其他地方,在 Apple 的实际实现文件中,UIImagePickerControllerOriginalImage实际定义如下:

NSString *const UIImagePickerControllerOriginalImage = @"someStringLiteral";

Apple 选择使用的实际字符串字面值可以是任何值,并且不一定必须与变量的名称相同。通过调用[info objectForKey:@"UIImagePickerControllerOriginalImage"],您基本上是在假设实际是UIImagePickerControllerOriginalImage @"UIImagePickerControllerOriginalImage"但不一定是这种情况。

我假设原始行导致返回值为nil,这意味着没有为 key 找到对象@"UIImagePickerControllerOriginalImage"。如果image是这样nil,那么其余的代码基本上会“默默地失败”。如果您按照代码进行操作,则基本上等同于执行以下操作:

self.facePicture.image = nil;

CIImage* image = [CIImage imageWithCGImage:NULL]; // likely returns nil

NSArray* features = [detector featuresInImage:nil]; //***features array is empty
NSLog(@"array count is %i", features.count); //returns 0
于 2013-04-10T01:59:22.673 回答