Skip to content

Instantly share code, notes, and snippets.

@Air-Craft
Created April 30, 2015 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Air-Craft/e9acc32dc27d6681394f to your computer and use it in GitHub Desktop.
Save Air-Craft/e9acc32dc27d6681394f to your computer and use it in GitHub Desktop.
Apply CIFilter to a CALayer #CoreImage #image-manipulation #ios
// SIMPLE
// Note: Can convert to UIImage but there's no way to go to CGImage.
CIImage *newImg = [[CIImage imageWithCGImage:img.CGImage] imageByApplyingFilter:@"CIColorMonochrome"
withInputParameters:@{kCIInputColorKey: ciCol}];
// EXTENDED
CIContext *ctx = [CIContext contextWithOptions:nil];
CIFilter *filter = [CIFilter filterWithName:@"CIColorMonochrome"];
CIColor *color = [CIColor colorWithRed:pitchTilt green:rollTilt blue:1.0 alpha:1.0];
[filter setDefaults];
[filter setValue:[CIImage imageWithCGImage:img.CGImage] forKey:kCIInputImageKey];
[filter setValue:color forKey:kCIInputColorKey];
CGImageRef cgImg = [ctx createCGImage:filter.outputImage fromRect:filter.outputImage.extent];
static CALayer *l;
if (!l) {
l = [CALayer layer];
l.frame = CGRectMake(0, 0, filter.outputImage.extent.size.width/2.0, filter.outputImage.extent.size.height/2.0);
[_instrumentPanel.layer addSublayer:l];
l.contentsScale = 2.0;
}
l.contents = (__bridge id)(cgImg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment