Applying a CIFilter to a CALayer
Asked Answered
G

1

6

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.

Gershom answered 14/3, 2012 at 11:47 Comment(2)
From the CALayer documentation: "iOS Note: While the CALayer class in iOS exposes the filters property, Core Image is not available. Currently the filters available for this property are undefined." - so I'm not sure if it is really workingAnastaciaanastas
Also, none of the blur filters from the Mac are currently supported in the Core Image implementation on iOS: #8529226Whereby
A
6

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:

Create the input CIImage from the contents of your layer:

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

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

Finally set the CGImageRef to the layer:

[myCircle setContents:(id)cgimg];

This should work :)

Addressee answered 24/3, 2012 at 20:18 Comment(8)
It depends, I think there is no much we can do inside the filter, what we could try is to draw directly to the layer using OpenGL instead of creating an CGImageRef and using setContents:.Addressee
Also we could file a bug-report and request setFilters: to be able to use, and sit and wait :p.Addressee
You'll probably need to do a CFBridgingRelease(cgimg) for ARC on that last line. Also need [filter setValue:inputImage forKey:@"inputImage"];Heaven
Oh... this answer needs an update. I think now is possible to apply filter directly.... I will update this answer later :) if not ping me again.Addressee
ping pong.. is it possible to set the filters directly?Adolescence
@nacho4d, Is it possible to apply filter directly??Alon
@Addressee Did you look up that, We're pinging to youTetany
Sorry I couldn't find the time to really dive into this. It is apparently possible. Look at this notes: github.com/shinobicontrols/iOS8-day-by-day/blob/master/…Addressee

© 2022 - 2024 — McMap. All rights reserved.