将CIFilter应用于CALayer [英] Applying a CIFilter to a CALayer

查看:179
本文介绍了将CIFilter应用于CALayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此, CI过滤器现在可在iOS 5中使用,我想将其应用于CALayer,您在Mac上的操作方式。这是我的代码:

So, CI Filters are now available in iOS 5, and I'm trying to apply one to a CALayer, the way you'd do it on Mac. Here's my code:

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];

我的白色圆圈绘制精美,但不应用过滤器。有什么想法吗?

My white circle draws fine, but the filter isn't applied. Any thoughts?

推荐答案

除了 CIDiskBlur iOS SDK 5.1),并且 setFilters:似乎不可用,您可以执行以下操作:

Aside from the fact that CIDiskBlur is not available (as of iOS SDK 5.1) and that setFilters: seems to be not available either you could do the following:

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

Create the input CIImage from the contents of your layer:

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

应用您的过滤器并在CGImageRef中获得结果:

Apply your filters and get the result in an 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设置为图层:

Finally set the CGImageRef to the layer:

[myCircle setContents:(id)cgimg];

这应该可以工作:)

这篇关于将CIFilter应用于CALayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆