Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a CIImage from CIColor in a CIFilter: getting empty image

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?

like image 210
Callmeed Avatar asked Jun 05 '12 18:06

Callmeed


Video Answer


2 Answers

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];
like image 94
Arsynth Avatar answered Oct 01 '22 21:10

Arsynth


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"];
like image 44
William Vasconcelos Avatar answered Oct 01 '22 19:10

William Vasconcelos