我正在尝试CIFilterGenerator
使用以下代码创建一个用于 CALayer 的:
CIFilterGenerator *generator = [[CIFilterGenerator alloc] init];
CIFilter *colorFilter = [CIFilter filterWithName:@"CIConstantColorGenerator"];
[colorFilter setDefaults];
[colorFilter setValue:[CIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:0.5] forKey:kCIInputColorKey];
CIFilter *cropFilter = [CIFilter filterWithName:@"CICrop"];
[cropFilter setDefaults];
[cropFilter setValue:[CIVector vectorWithX:0.0 Y:0.0 Z:1.0 W:1.0] forKey:@"inputRectangle"];
[generator connectObject:colorFilter withKey:kCIOutputImageKey toObject:cropFilter withKey:kCIInputImageKey];
CIFilter *colorMap = [CIFilter filterWithName:@"CIColorMap"];
[colorMap setDefaults];
[generator connectObject:cropFilter withKey:kCIOutputImageKey toObject:colorMap withKey:kCIInputGradientImageKey];
CIFilter *blur = [CIFilter filterWithName:@"CIGaussianBlur"];
[blur setDefaults];
[blur setValue:@10.0 forKey:kCIInputRadiusKey];
[generator connectObject:colorMap withKey:kCIOutputImageKey toObject:blur withKey:kCIInputImageKey];
[generator exportKey:kCIInputColorKey fromObject:colorFilter withName:nil];
[generator exportKey:kCIInputImageKey fromObject:colorMap withName:nil];
[generator exportKey:kCIOutputImageKey fromObject:blur withName:nil];
但是,当我尝试将其附加到图层时,出现异常:
/SourceCache/CoreImage/CoreImage-8.2.2/API/CIFilter.mm:588: CIColorMap apply:: Caught exception: CIColorMap: colorMap: nil value for argument #0 (src)
这很奇怪,因为我正在导出颜色映射输入图像。更奇怪的是,如果我将导出的输出图像更改为颜色图,它会按预期工作(当然减去模糊)。