Using a CIImage from CIColor in a CIFilter: getting empty image
Asked Answered
O

2

5

I'm trying to create a CIFilter with blend mode (like overlay or multiply). Relevant code:

// Let's try a filter here
// Get the data
NSData *imageData = UIImageJPEGRepresentation(image, 0.85);
// Create a CI Image
CIImage *beginImage = [CIImage imageWithData:imageData];
CIImage *overlay = [CIImage imageWithColor:[CIColor colorWithRed:0.7 green:0.75 blue:0.9 alpha:0.75]];
// Create a context
CIContext *context = [CIContext contextWithOptions:nil];
// Create filter
CIFilter *filter = [CIFilter filterWithName:@"CIOverlayBlendMode" 
                                  keysAndValues:@"inputImage", beginImage,
                                                @"inputBackgroundImage", overlay,
                                                nil];

Other filters are working okay (like sepia tone), but with a filter that requires a "inputBackgroundImage" key, I get a blank/empty result ... so something seems to be wrong with my background image.

How do I use a blend mode filter by placing a solid color over an image?

Oler answered 5/6, 2012 at 18:47 Comment(1)
Have you verified that the overlay image is being generated properly when you use -imageWithColor:? Is it non-nil? Can you create a UIImage from it that looks correct?Rubato
K
6

Its okay in topic starter's code, only one fix should be.

NSData *imageData = UIImageJPEGRepresentation(image, 0.85);
// Create a CI Image
CIImage *beginImage = [CIImage imageWithData:imageData];
CIImage *overlay = [CIImage imageWithColor:[CIColor colorWithRed:0.7 green:0.75 blue:0.9 alpha:0.75]];

//You should crop the generated image to beginImage size because generated image's size is infinite. I don't know why, but CIOverlayBlendMode isn't tolerant to infinite size
    overlay = [overlay imageByCroppingToRect:CGRectMake(beginImage.extent.origin.x, beginImage.extent.origin.y, beginImage.extent.size.width, beginImage.extent.size.height)];

// Create a context
CIContext *context = [CIContext contextWithOptions:nil];
// Create filter
CIFilter *filter = [CIFilter filterWithName:@"CIOverlayBlendMode" 
                                      keysAndValues:@"inputImage", beginImage,
                                                    @"inputBackgroundImage", overlay,
                                                    nil];
Kauslick answered 14/2, 2014 at 19:19 Comment(0)
P
3

This works:

//assume beginImage is CIImage you want to tint            
CIImage* outputImage = nil;

//create some blue
CIFilter* blueGenerator = [CIFilter filterWithName:@"CIConstantColorGenerator"];
CIColor* blue = [CIColor colorWithString:@"0.1 0.5 0.8 1.0"];
[blueGenerator setValue:blue forKey:@"inputColor"];
CIImage* blueImage = [blueGenerator valueForKey:@"outputImage"];

//apply a multiply filter
CIFilter* filterm = [CIFilter filterWithName:@"CIMultiplyCompositing"];
[filterm setValue:blueImage forKey:@"inputImage"];
[filterm setValue:beginImage forKey:@"inputBackgroundImage"];
outputImage = [filterm valueForKey:@"outputImage"];
Perpendicular answered 1/10, 2012 at 16:3 Comment(1)
To improve the quality of your post, please include how/why this code will solve the problem.Chretien

© 2022 - 2024 — McMap. All rights reserved.