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?
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];
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"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With