无法生成蒙版图像; CISourceOverCompositing无法按预期工作

时间:2013-02-24 22:01:22

标签: ios objective-c ios6 ios-simulator core-image

我基本上试图让"Anonymous Faces Filter Recipe" example在iOS模拟器中运行。

没有错误或警告,但似乎“CISourceOverCompositing”过滤器未按预期工作。圆圈正在破坏性地绘制在maskImage上而不是相加。例如,如果找到5个面,则绘制5个圆,但只有最后一个显示在maskImage上。

如何才能使每个圆圈以加法方式绘制到maskImage?

以下是我用来构建掩码的代码:

// Create a green circle to cover the rects that are returned.
CIImage *maskImage = nil;
for (CIFeature *f in faces){
    CIVector *circleCenter = [CIVector vectorWithX:f.bounds.origin.x+f.bounds.size.width/2.0 Y:f.bounds.origin.y+f.bounds.size.height/2.0];
    CGFloat circleRadius = MIN([f bounds].size.width, [f bounds].size.height)/1.5;
    CIFilter *radialGradient = [CIFilter filterWithName:@"CIRadialGradient" keysAndValues:
                                @"inputRadius0", [NSNumber numberWithFloat:circleRadius],
                                @"inputRadius1", [NSNumber numberWithFloat:circleRadius+1.0f],
                                @"inputColor0", [CIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0],
                                @"inputColor1", [CIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],
                                @"inputCenter", circleCenter, nil];
    CIImage *circleImage = [radialGradient valueForKey:@"outputImage"];
    if (maskImage == nil) {
        maskImage = circleImage;
    } else {
        maskImage = [[CIFilter filterWithName:@"CISourceOverCompositing" keysAndValues:@"inputImage", circleImage, @"inputBackgroundImage",maskImage, nil] valueForKey:@"outputImage"];
    }
}

1 个答案:

答案 0 :(得分:4)

不幸的是,Anonymous Faces Filter Recipe上找到的源代码没有编译或开箱即用。可以使用inputColor1参数和Alpha通道找到 CISourceOverCompositing 过滤器的问题。用以下内容替换inputColor1颜色,你应该好好去:

@"inputColor1", [CIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]