Applying a CIFilter to a CALayer

前端 未结 1 716
天命终不由人
天命终不由人 2020-12-15 01:50

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         


        
相关标签:
1条回答
  • 2020-12-15 02:31

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

    0 讨论(0)
提交回复
热议问题