将CIFilter应用于CALayer

问题描述 投票:5回答:1

CI Filters现在可以在iOS 5中使用,我正在尝试将其应用于CALayer,the way you'd do it on Mac。这是我的代码:

CALayer *myCircle = [CALayer layer];
myCircle.bounds = CGRectMake(0,0,30,30);
myCircle.position = CGPointMake(100,100);
myCircle.cornerRadius = 15;
myCircle.borderColor = [UIColor whiteColor].CGColor;
myCircle.borderWidth = 2;
myCircle.backgroundColor = [UIColor whiteColor].CGColor;

CIFilter *blurFilter = [CIFilter filterWithName:@"CIDiscBlur"];
[blurFilter setDefaults];
[blurFilter setValue:[NSNumber numberWithFloat:5.0f] forKey:@"inputRadius"];
[myCircle setFilters:[NSArray arrayWithObjects:blurFilter, nil]];

[self.view.layer addSublayer:myCircle];

我的白色圆圈画得很好,但是没有应用滤镜。

ios cocoa-touch calayer core-image
1个回答
6
投票

除了CIDiskBlur不可用的事实(从iOS SDK 5.1开始)并且setFilters:似乎不可用,您可以执行以下操作:

从图层的内容创建输入CIImage:

CIImage *inputImage = [CIImage imageWithCGImage:(CGImageRef)(myCircle.contents)];`

应用您的过滤器并将结果输入CGImageRef:

CIFilter *filter = [CIFilter filterWith...];// A filter that is available in iOS or a custom one :)
...
CIImage *outputImage = [filter outputImage];
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];

最后将CGImageRef设置为图层:

[myCircle setContents:(id)cgimg];

这应该工作:)

© www.soinside.com 2019 - 2024. All rights reserved.