Applying a CIFilter to a CALayer

13,904

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 :)

Share:
13,904
Eric
Author by

Eric

I got keys coming from KVC Cost a newbie 200 leaks I'm a Swift commando, Xcode for example This coding lifestyle is hard to handle So I go debug cause I'm more like a test player Thug, branded in the business-layer So many agile haters, imitators steady swanging Make me wanna start break pointing

Updated on July 23, 2022

Comments

  • Eric
    Eric almost 2 years

    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.

  • nacho4d
    nacho4d over 10 years
    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:.
  • nacho4d
    nacho4d over 10 years
    Also we could file a bug-report and request setFilters: to be able to use, and sit and wait :p.
  • JaredH
    JaredH over 9 years
    You'll probably need to do a CFBridgingRelease(cgimg) for ARC on that last line. Also need [filter setValue:inputImage forKey:@"inputImage"];
  • nacho4d
    nacho4d about 9 years
    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.
  • Fawkes
    Fawkes almost 9 years
    ping pong.. is it possible to set the filters directly?
  • Mrugesh Tank
    Mrugesh Tank over 8 years
    @nacho4d, Is it possible to apply filter directly??
  • kokos8998
    kokos8998 about 8 years
    @nacho4d Did you look up that, We're pinging to you
  • nacho4d
    nacho4d about 8 years
    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/…