Using CoreImage filters

Guides | Tutorial By 4 years ago

CoreImage has some pretty neat APIs. A lot of them aren’t very well known. I’m going to show off how easy it is to use the image filters, which can be used to add effects to an image.

I’ll demonstrate the dot filter, but there are many others which I have listed at the bottom.

First you need to import and link with the CoreImage.framework

#import 

Now the class for adding the effects is CIFilter. Open your image and make a CIImage version of it (specifically for CoreImage use). Then make a CIFilter of “CIDotScreen”, this is the type of filter that will be created.

UIImage* inputImage = [UIImage imageNamed:@"image.jpg"];
CIImage* filterInputImage = [CIImage imageWithCGImage:inputImage.CGImage];

CIFilter* filter = [CIFilter filterWithName:@"CIDotScreen"];

Now input the image and get the output image (which is processed with the filter).

[filter setValue:filterInputImage forKey:kCIInputImageKey];

CIImage* filterOutputImage = filter.outputImage;

To make it back into a UIImage, a CIContext needs to be used, as so

CIContext* ctx = [CIContext contextWithOptions:nil];
CGImageRef createdImage = [ctx createCGImage:filterOutputImage fromRect:filterOutputImage.extent];

UIImage* outputImage = [UIImage imageWithCGImage:createdImage];

CGImageRelease(createdImage);
createdImage = nil;

And that’s it. Here’s a list of all the filters, that can be used in place of “CIDotScreen”.

NSLog(@"%@", [CIFilter filterNamesInCategory:kCICategoryBuiltIn]);

(
	CIAdditionCompositing,
	CIAffineClamp,
	CIAffineTile,
	CIAffineTransform,
	CIBarsSwipeTransition,
	CIBlendWithMask,
	CIBloom,
	CIBumpDistortion,
	CIBumpDistortionLinear,
	CICheckerboardGenerator,
	CICircleSplashDistortion,
	CICircularScreen,
	CIColorBlendMode,
	CIColorBurnBlendMode,
	CIColorControls,
	CIColorCube,
	CIColorDodgeBlendMode,
	CIColorInvert,
	CIColorMap,
	CIColorMatrix,
	CIColorMonochrome,
	CIColorPosterize,
	CIConstantColorGenerator,
	CICopyMachineTransition,
	CICrop,
	CIDarkenBlendMode,
	CIDifferenceBlendMode,
	CIDisintegrateWithMaskTransition,
	CIDissolveTransition,
	CIDotScreen,
	CIEightfoldReflectedTile,
	CIExclusionBlendMode,
	CIExposureAdjust,
	CIFalseColor,
	CIFlashTransition,
	CIFourfoldReflectedTile,
	CIFourfoldRotatedTile,
	CIFourfoldTranslatedTile,
	CIGammaAdjust,
	CIGaussianBlur,
	CIGaussianGradient,
	CIGlideReflectedTile,
	CIGloom,
	CIHardLightBlendMode,
	CIHatchedScreen,
	CIHighlightShadowAdjust,
	CIHoleDistortion,
	CIHueAdjust,
	CIHueBlendMode,
	CILanczosScaleTransform,
	CILightenBlendMode,
	CILightTunnel,
	CILinearGradient,
	CILineScreen,
	CILuminosityBlendMode,
	CIMaskToAlpha,
	CIMaximumComponent,
	CIMaximumCompositing,
	CIMinimumComponent,
	CIMinimumCompositing,
	CIModTransition,
	CIMultiplyBlendMode,
	CIMultiplyCompositing,
	CIOverlayBlendMode,
	CIPinchDistortion,
	CIPixellate,
	CIRadialGradient,
	CIRandomGenerator,
	CISaturationBlendMode,
	CIScreenBlendMode,
	CISepiaTone,
	CISharpenLuminance,
	CISixfoldReflectedTile,
	CISixfoldRotatedTile,
	CISmoothLinearGradient,
	CISoftLightBlendMode,
	CISourceAtopCompositing,
	CISourceInCompositing,
	CISourceOutCompositing,
	CISourceOverCompositing,
	CIStarShineGenerator,
	CIStraightenFilter,
	CIStripesGenerator,
	CISwipeTransition,
	CITemperatureAndTint,
	CIToneCurve,
	CITriangleKaleidoscope,
	CITwelvefoldReflectedTile,
	CITwirlDistortion,
	CIUnsharpMask,
	CIVibrance,
	CIVignette,
	CIVortexDistortion,
	CIWhitePointAdjust
)
  • blackjack75

    You forgot to escape the CoreImage/CoreImage above and it is treated as a HTML tag so it becomes invisible.