Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get Core Image filter such as CILinearGradient to work?

I am new to Core Image and am trying out some of the filters. I can get some of the filters to work but others just refused to work.

For example, I can get the CIGaussianGradient filter to work using codes like this:

CIVector *centerVector = [CIVector vectorWithX:150 Y:150];
CIColor *color0 = [CIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:1.0];
CIColor *color1 = [CIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
NSNumber *radius = [NSNumber numberWithFloat:300.0];

CIImage *theCIImage = [CIFilter filterWithName:@"CIGaussianGradient" keysAndValues:@"inputCenter", centerVector, @"inputColor0", color0, @"inputColor1", color1, @"inputRadius", radius, nil].outputImage;  

 UIImage *newImage = [UIImage imageWithCIImage: theCIImage];
self.ImgView.image = newImage;

But I cannot get the CILinearGradient to work using simiar code such as this:

CIVector *point0 =[CIVector vectorWithX:0 Y:0];
CIVector *point1 =[CIVector vectorWithX:200 Y:200];
CIColor *color0 = [CIColor colorWithRed:1.0 green:0.0 blue:1.0 alpha:1.0];
CIColor *color1 = [CIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];

CIImage *theCIImage = [CIFilter filterWithName:@"CILinearGradient" keysAndValues:@"inputPoint0", point0, @"inputPoint1", point1, @"inputColor0", color0, @"inputColor1", color1, nil].outputImage;
 UIImage *newImage = [UIImage imageWithCIImage: theCIImage];
self.ImgView.image = newImage;

What am I missing?

I am testing it on iOS 6.0.

like image 487
ray Avatar asked Feb 12 '13 07:02

ray


1 Answers

This one worked for me on iOS 7.

CIContext *coreImageContext = [CIContext contextWithOptions:nil];
CIFilter *gradientFilter = [CIFilter filterWithName:@"CILinearGradient"];
[gradientFilter setDefaults];
CIColor *startColor = [CIColor colorWithCGColor:[UIColor yellowColor].CGColor];
CIColor *endColor = [CIColor colorWithCGColor:[UIColor redColor].CGColor];
CIVector *startVector = [CIVector vectorWithX:0 Y:0];
CIVector *endVector = [CIVector vectorWithX:100 Y:100];
[gradientFilter setValue:startVector forKey:@"inputPoint0"];
[gradientFilter setValue:endVector forKey:@"inputPoint1"];
[gradientFilter setValue:startColor forKey:@"inputColor0"];
[gradientFilter setValue:endColor forKey:@"inputColor1"];
CIImage *outputImage = [gradientFilter outputImage];
CGImageRef cgimg = [coreImageContext createCGImage:outputImage fromRect:CGRectMake(0, 0, 100, 100)];
UIImage *newImage = [UIImage imageWithCGImage:cgimg];
self.ImgView.image = newImage;
like image 186
Genki Avatar answered Sep 24 '22 05:09

Genki