Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Cannot process image using CIFilter

I am trying to process image using Core Image. I have created UIImage category to do it.

I have added QuartzCore and CoreImage frameworks to project, imported CoreImage/CoreImage.h and used this code:

CIImage *inputImage = self.CIImage;
CIFilter *exposureAdjustmentFilter = [CIFilter filterWithName:@"CIExposureAdjust"];
[exposureAdjustmentFilter setDefaults];
[exposureAdjustmentFilter setValue:inputImage forKey:@"inputImage"];
[exposureAdjustmentFilter setValue:[NSNumber numberWithFloat:5.0f] forKey:@"inputEV"];
CIImage *outputImage = [exposureAdjustmentFilter valueForKey:@"outputImage"];
CIContext *myContext = [CIContext contextWithOptions:nil];

return [UIImage imageWithCGImage:[myContext createCGImage:outputImage fromRect:outputImage.extent]];

But I have got nil output image from the filter.

I have also tried to use CIHueAdjust with the same result.

Than you in advance

UPDATE: I have found solution. It was necessary to alloc new CIImage, not only pass reference to UIImage.CIImage this way:

CIImage *inputImage = [[CIImage alloc] initWithImage:self];
like image 568
Martin Pilch Avatar asked Dec 15 '11 11:12

Martin Pilch


1 Answers

Try following code:-

 CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"old-country-rain.jpg"]];
CIFilter * controlsFilter = [CIFilter filterWithName:@"CIExposureAdjust"];
[controlsFilter setValue:inputImage forKey:kCIInputImageKey];
[controlsFilter setValue:[NSNumber numberWithFloat: 2.0f] forKey:@"inputEV"];
NSLog(@"%@",controlsFilter.attributes);
CIImage *displayImage = controlsFilter.outputImage;
UIImage *finalImage = [UIImage imageWithCIImage:displayImage];

CIContext *context = [CIContext contextWithOptions:nil];
if (displayImage == nil || finalImage == nil) {
    // We did not get output image. Let's display the original image itself.
    imageView.image = [UIImage imageNamed:@"old-country-rain.jpg"];
}else {
    // We got output image. Display it.
    imageView.image = [UIImage imageWithCGImage:[context createCGImage:displayImage fromRect:displayImage.extent]];
}
like image 92
Leena Avatar answered Oct 23 '22 17:10

Leena