iOS7: CIFilter to UIImage
"Core Image is an image processing and analysis technology designed to provide near real-time processing for still and video images."
"The CIFilter class produces a CIImage object as output. Typically, a filter takes one or more images as input.ย
CIFilter object are set and retrieved through the use of key-value pairs.
If you app is multithreaded, each thread must create its own CIFilter object. Otherwise, your app could behave unexpectedly".
So, apply a CIFilter to a image is really easy. You can create a chain of filters too.
With iOS7 there are more new filters that you can use in a image.
See the list below:
filters = @[ @"Original", @"CILinearToSRGBToneCurve", @"CIPhotoEffectChrome", @"CIPhotoEffectFade", @"CIPhotoEffectInstant", @"CIPhotoEffectMono", @"CIPhotoEffectNoir", @"CIPhotoEffectProcess", @"CIPhotoEffectTonal", @"CIPhotoEffectTransfer", @"CISRGBToneCurveToLinear", @"CIVignetteEffect", ];
Then, you can apply a filter to a UIImage using the code below.ย
The originalImage is the original UIImage object; the self.imgView.image is the final UIImage.
Setting the chain boolean at true will apply the new filter to a already filtered image.
if(index == 0) { self.imgView.image = originalImage; return; } NSString *titleFilter = filters[index]; CIImage *ciimage = [[CIImage alloc] initWithImage:chain ? self.imgView.image : originalImage]; CIFilter *filter = [CIFilter filterWithName:titleFilter keysAndValues:kCIInputImageKey, ciimage, nil]; //Creates a CIFilter object for a specific kind of filter and initializes the input values. [filter setDefaults]; //reset filter to default values CIContext *context = [CIContext contextWithOptions:nil]; CIImage *output = [filter outputImage]; //apply filter and return a CIImage CGImageRef cgimage = [context createCGImage:output fromRect:[output extent]]; self.imgView.image = [UIImage imageWithCGImage:cgimage scale:0 orientation:[self.imgView.image imageOrientation]]; CGImageRelease(cgimage);