5

我是 Core Image 的新手,正在尝试一些过滤器。我可以让一些过滤器工作,但其他人只是拒绝工作。

例如,我可以使用如下代码让 CIGaussianGradient 过滤器工作:

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;

但我无法使用类似的代码让 CILinearGradient 工作,例如:

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;

我错过了什么?

我正在 iOS 6.0 上测试它。

4

1 回答 1

5

这个在 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;
于 2014-02-09T02:38:02.933 回答