Objective-C : No Matter what I do CIDetector is always nil

旧巷老猫 提交于 2019-12-06 05:16:20

I'm assuming you're using this article, because I was too and had the same problem. There's actually a bug in his code. The CIDetector instantiation should look like:

CIDetector *smileDetector = [CIDetector detectorOfType:CIDetectorTypeFace
                            context:context 
                            options:@{CIDetectorTracking: @YES, 
                                      CIDetectorAccuracy: CIDetectorAccuracyLow}];

Note that the detector type is CIDetectorTypeFace, rather than CIDetectorSmile. CIDetectorSmile is a feature option rather than a detector type, so to extract just the smiles (and not all the faces), use this code:

NSArray *features = [smileDetector featuresInImage:image options:@{CIDetectorSmile: @YES}];
CIDetector *smileDetector = [CIDetector detectorOfType:CIDetectorTypeFace
                            context:context 
                            options:@{CIDetectorTracking: @YES, 
                                      CIDetectorAccuracy: CIDetectorAccuracyLow}];
NSArray *features = [smileDetector featuresInImage:image options:@{CIDetectorSmile:@YES}];
if (([features count] > 0) && (((CIFaceFeature *)features[0]).hasSmile)) {
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(didFinishWritingImage), features);
} else {
    self.label.text = @"Say Cheese!"
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!