问题
I am looking for a code sample of this Core Image filter for iOS. Those filters with the inputImage in the parameter, I can figure out how to implement. But the ones without the inputImage parameter, I am not sure how it works.
Here is the extract from Apple's doc:
CIGaussianGradient
Generates a gradient that varies from one color to another using a Gaussian distribution. Parameters
inputCenter
A CIVector class whose attribute type is CIAttributeTypePosition and whose display name is Center.
Default value: [150 150] Identity: (null)
inputColor0
A CIColor class whose display name is Color 1.
inputColor1
A CIColor class whose display name is Color 2.
inputRadius
An NSNumber class whose attribute type is CIAttributeTypeDistance and whose display name is Radius.
Default value: 300.00 Minimum: 0.00 Maximum: 0.00 Slider minimum: 0.00 Slider maximum: 800.00 Identity: 300.00
回答1:
This should get you started... though I'm not sure why this produces a magenta over white gradient in iOS, while it produces a magenta over black gradient in Quartz Composer. (if you haven't used Quartz Composer, it's included with Apple's dev tools and is great for testing out Core Image filters)
To do anything useful with it, I believe you have to crop it afterwards - otherwise it will have infinite dimensions (according to Quartz Composer).
// set up the parameters for the filter
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];
// create a CIImage and apply the filter
CIImage *theCIImage = [[CIImage alloc] init];
theCIImage = [CIFilter filterWithName:@"CIGaussianGradient" keysAndValues:@"inputCenter", centerVector, @"inputColor0", color0, @"inputColor1", color1, @"inputRadius", radius, nil].outputImage;
// crop the image using CICrop
CGRect rect = CGRectMake(0.0, 0.0, 600.0, 600.0);
theCIImage = [CIFilter filterWithName:@"CICrop" keysAndValues:kCIInputImageKey, theCIImage, @"inputRectangle", rect, nil].outputImage;
来源:https://stackoverflow.com/questions/8031785/an-example-use-of-cigaussiangradient-filter-for-core-image