One month later ...
This is an example of CIColorMatrix
setting all its parameters :)
-(void)doCIColorMatrixFilter
{
// Make the input image recipe
CIImage *inputImage = [CIImage imageWithCGImage:[UIImage imageNamed:@"facedetectionpic.jpg"].CGImage]; // 1
// Make the filter
CIFilter *colorMatrixFilter = [CIFilter filterWithName:@"CIColorMatrix"]; // 2
[colorMatrixFilter setDefaults]; // 3
[colorMatrixFilter setValue:inputImage forKey:kCIInputImageKey]; // 4
[colorMatrixFilter setValue:[CIVector vectorWithX:1 Y:1 Z:1 W:0] forKey:@"inputRVector"]; // 5
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:1 Z:0 W:0] forKey:@"inputGVector"]; // 6
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:1 W:0] forKey:@"inputBVector"]; // 7
[colorMatrixFilter setValue:[CIVector vectorWithX:0 Y:0 Z:0 W:1] forKey:@"inputAVector"]; // 8
// Get the output image recipe
CIImage *outputImage = [colorMatrixFilter outputImage]; // 9
// Create the context and instruct CoreImage to draw the output image recipe into a CGImage
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]]; // 10
// Draw the image in screen
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageWithCGImage:cgimg]];
CGRect f = imageView2.frame;
f.origin.y = CGRectGetMaxY(imageView.frame);
imageView2.frame = f;
[self.view addSubview:imageView2];
}
So this is what the sample does:
In 1
we create the ciimage, if you are getting nil there then make sure you are passing the right UIImage/CGImage or path.
In 2
create the filter, you know this :)
In 3
set the filter parameters to its defaults, CoreImage Programming guide suggests we should do this although (I haven't experimented any strange/bad things if avoided.)
In 4
set the input ciimage
From 5
through 8
we set the parameters. For example I made the red vector {1,1,1,0} so the image looks reddish. 6
, 7
and 8
and not necessary here since their values are the same as the defaults (remember we called -setDefaults
?) but for educational purposes I guess they are fine :)
In 9
set the output image, although is not drawn yet.
Finally in 10
you tell CoreImage to draw the output image into a CGImage, and we put that CGImage into an UIImage and it inside an UIImageView.
This is the result (I used the same image as this tutorial):
Hope it helps.