Using custom CIFilter on CALayer shows no change to CALayer
As @DonMag points out it should have worked with the changes he described. How ever unfortunately we heard back from Apple today;
At this time, there is a bug preventing custom CIFilters on a CALayer from working. There is no workaround for this bug at this time.
When we file the bug I will add the link here for those interested. But at this time you can not add a custom CIFilter to a CALayer on macOS 11.
Let’s hope they fix it for all of you reading this for a solution.
EDIT:
So bad news... currently on macOS 12.2.1, and it still has the same issue, nothing has happened based on our ticket. Doesn't seem like Apple want's to fix this. For those of you out there looking: This still does NOT work on a CALayer
even with all the options on like described in the other answers. A builtin CIFilter
works as expected.
Note that using the same custom CIFilter
on a CALayer
for an export using AVVideoCompositionCoreAnimationTool
does work!
How can I get a custom CIFilter to work on a CALayer for export using the AVVideoCompositionCoreAnimationTool
There seem to be 2 things that are important to add in order for CALayers
to work with the custom CIFilter
.
- You need to override the inputKeys:
override var inputKeys: [String] {
return [kCIInputImageKey, "value"]
}
- You need to declare all used parameters like this:
override var attributes: [String : Any] {
return [
kCIAttributeFilterDisplayName: "Custom Filter",
kCIAttributeFilterName: "CustomFilter",
kCIAttributeFilterCategories: [
kCICategoryVideo,
kCICategoryStillImage,
"CustomFilters",
],
kCIInputImageKey: [
kCIAttributeIdentity: 0,
kCIAttributeClass: "CIImage",
kCIAttributeDisplayName: "Image",
kCIAttributeType: kCIAttributeTypeImage
],
#keyPath(value): [
kCIAttributeIdentity: 0,
kCIAttributeClass: "NSNumber",
kCIAttributeDisplayName: "Custom value",
kCIAttributeMin: 0,
kCIAttributeMax: 5,
kCIAttributeSliderMin: 0,
kCIAttributeSliderMax: 5,
kCIAttributeType: kCIAttributeTypeScalar
],
kCIOutputImageKey: [
kCIAttributeClass: "CIImage",
kCIAttributeDisplayName: "Output Image",
kCIAttributeType: kCIAttributeTypeImage
]
]
}
Hope I save some of you loads of time figuring this one out!
Adding CIFilter to CALayer under Mavericks?
Figured it out, Apple decided to change this and require a new flag for no reason
progressIndicator.layerUsesCoreImageFilters = YES;
How to add CIFilter to CALayer
There is no CIFilter in iPhone OS. There is CAFilter but it is a private API so using it on AppStore apps means rejection. If you already know what images you will use, you could create the blurred image in the first place.
See How to implement a box or gaussian blur on iPhone on how to implement a Gaussian blur filter "legally".
See CAFilter — iPhone Development Wiki on how to apply a blur filter on a CALayer using the private CAFilter class.
How to use a CIFilter on the layerClass instance of an UIView?
This is not possible on iOS. From the CALayer
class reference:
Special Considerations
This property is not supported on layers in iOS.
Presumably Apple don't feel that the current generation of iOS hardware is powerful enough to support live image filtering.
Applying a CIFilter to a CALayer
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 :)
Related Topics
Protocol Extension Initializer Forcing to Call Self.Init
How to Keep a Reference to Another Object in the Parameters of the Class
Nsdateformatter Detect 24-Hour Clock in Os X and iOS
Xcode Takes Long Time to Print Debug Results
How to Initialise a New Nsdocument Instance in Swift
Nsposixerrordomain When Binding to Socket on MACos 10.12
iOS Swift Error: 'T' Is Not Convertible to 'Mirrordisposition'
How to Sort JSON Coming from Alamofire and Return Final JSON Object (Swiftyjson)
Hide Status Bar in Launch Screen
Using Animoji/Memoji as Profile Photo
Prevent Redirect Response with Alamofire in Swift
Swiftui, Shadow Only for Container
Alamofire 5 Upload Encodingcompletion
Using @Fetchrequest(Entity: ) for Swiftui MACos App Crashes
How to I Access Argv and Argc in Swift