Search code examples
ioscore-imagecgimagecifiltercgimageref

How can I create a finite CIImage?


The CIColorMap CIFilter takes a CIImage as an input. It makes sense to generate that input image dynamically using another CIFilter (such as CILinearGradient) but providing the result of a filter to the color map filter will result in the following error like the following:

[CIColorMap outputImage] requires the inputGradientImage to be finite

Even though the start/end points of the gradient are specified as inputs to the CILinearGradient filter the image itself is not finite. How can I generate a finite CIImage to pass to my filter?


Solution

  • Here's an example of how to create a fixed size CIImage to use as an input to another CIFilter:

    CGFloat gradientSize = 100.f;         
    CIFilter *gradientFilter = [CIFilter filterWithName:@"CISmoothLinearGradient"];
    [gradientFilter setValue:[CIVector vectorWithX:0 Y:0] forKey:@"inputPoint0"];
    [gradientFilter setValue:[CIVector vectorWithX:gradientSize Y:0] forKey:@"inputPoint1"];
    [gradientFilter setValue:[CIColor colorWithCGColor:[[UIColor cyanColor] CGColor]] forKey:@"inputColor0"];
    [gradientFilter setValue:[CIColor colorWithCGColor:[[UIColor magentaColor] CGColor]] forKey:@"inputColor1"];
    CIImage *gradientImage = [[gradientFilter outputImage] imageByCroppingToRect:CGRectMake(0, 0, gradientSize, 1)];
    
    // Other filter to receive cropped gradientImage
    CIFilter *gradientMapFilter = [CIFilter filterWithName:@"CIColorMap"];
    [gradientMapFilter setValue:gradientImage forKey:@"inputGradientImage"];