我正在试图弄清楚如何从用户的相机胶卷中获取图像并在其上运行一些核心图像操作(检测面部)。我能够成功地将图像加载到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
}
答案 0 :(得分:1)
尝试更改以下行
UIImage *image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
为:
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
重要的部分是从使用@"UIImagePickerControllerOriginalImage"
更改为使用UIImagePickerControllerOriginalImage
。原因是UIImagePickerControllerOriginalImage
被声明如下:
extern NSString *const UIImagePickerControllerOriginalImage;
在其他地方,在Apple的实际实现文件中,将会出现UIImagePickerControllerOriginalImage
实际定义的内容:
NSString *const UIImagePickerControllerOriginalImage = @"someStringLiteral";
Apple选择使用的实际字符串文字值可以是任何值,并且不一定必须与变量的名称相同。通过调用[info objectForKey:@"UIImagePickerControllerOriginalImage"]
,您基本上假设UIImagePickerControllerOriginalImage
实际上是<{strong> @"UIImagePickerControllerOriginalImage"
,这不一定是这种情况。
我假设原始行导致返回值nil
,这意味着找不到键@"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