I am using Core Image and would like to produce a black and white effect on the chosen image.
Ideally I would like to have access to the same sort of options that are available on Photoshop i.e. Reds, Cyan, Greens, Blues and Magenta. The goal being to create different types of the black and white effect.
Does anyone know what filter would be best to manipulate these sort of options? If not does anyone know of a good approach to creating the black and white effect using other filters?
Thanks
Oliver
Overview. The CIContext class provides an evaluation context for Core Image processing with Quartz 2D, Metal, or OpenGL. You use CIContext objects in conjunction with other Core Image classes, such as CIFilter , CIImage , and CIColor , to process images using Core Image filters.
A CIImage is a immutable object that represents an image. It is not an image. It only has the image data associated with it. It has all the information necessary to produce an image. You typically use CIImage objects in conjunction with other Core Image classes such as CIFilter, CIContext, CIColor, and CIVector.
Here's what you need in your subclass: class CustomFilter:CIFilter { let kernel = CIColorKernel(source: "kernel vec4 brightenEffect (sampler src , float k) { \n " + "vec4 currentSource = sample (src, samplerCoord (src));" + "currentSource. rgb = currentSource. rgb + k * currentSource.
- (UIImage *)imageBlackAndWhite { CIImage *beginImage = [CIImage imageWithCGImage:self.CGImage]; CIImage *blackAndWhite = [CIFilter filterWithName:@"CIColorControls" keysAndValues:kCIInputImageKey, beginImage, @"inputBrightness", [NSNumber numberWithFloat:0.0], @"inputContrast", [NSNumber numberWithFloat:1.1], @"inputSaturation", [NSNumber numberWithFloat:0.0], nil].outputImage; CIImage *output = [CIFilter filterWithName:@"CIExposureAdjust" keysAndValues:kCIInputImageKey, blackAndWhite, @"inputEV", [NSNumber numberWithFloat:0.7], nil].outputImage; CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef cgiimage = [context createCGImage:output fromRect:output.extent]; //UIImage *newImage = [UIImage imageWithCGImage:cgiimage]; UIImage *newImage = [UIImage imageWithCGImage:cgiimage scale:image.scale orientation:image.imageOrientation]; CGImageRelease(cgiimage); return newImage; }
Upd.: For iOS6
there is CIColorMonochrome
filter, but I played with it and found it not so good as mine.
here is example with CIColorMonochrome
- (UIImage *)imageBlackAndWhite { CIImage *beginImage = [CIImage imageWithCGImage:self.CGImage]; CIImage *output = [CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:kCIInputImageKey, beginImage, @"inputIntensity", [NSNumber numberWithFloat:1.0], @"inputColor", [[CIColor alloc] initWithColor:[UIColor whiteColor]], nil].outputImage; CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef cgiimage = [context createCGImage:output fromRect:output.extent]; UIImage *newImage = [UIImage imageWithCGImage:cgiimage scale:self.scale orientation:self.imageOrientation]; CGImageRelease(cgiimage); return newImage; }
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