I'm trying to create a subclass of CIFilter
as follows:
class ColorMonochromeFilter: CIFilter {
required init(red: CGFloat!, green: CGFloat!, blue: CGFloat!) {
super.init(name: "ColorMonochrome")
setDefaults()
let colour = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
setValue(colour, forKey: kCIInputColorKey)
}
}
I get the following error messages:
It seems that the designated initialiser is init(coder aDecoder: NSCoder!)
due to CIFilter
conforming to NSCoding
protocol, rather than init(name: String!)
which is declared in an extension on CIFilter
.
I can remove the first error by adding:
required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
(although that seems a little superfluous!)
Is there a way to resolve the second issue?
What I'm trying to achieve here is the Swift equivalent of this:
@implementation ASHColorMonochromeFilter
+ (ASHColorMonochromeFilter *) filterWithRed: (CGFloat) red green:(CGFloat)green blue:(CGFloat)blue
{
ASHColorMonochromeFilter * filter = (ASHColorMonochromeFilter *)[ASHColorMonochromeFilter filterWithName: @"CIColorMonochrome"];
UIColor * color = [UIColor colorWithRed: red green: green blue: blue alpha: 1.0];
[filter setValue: color forKey: kCIImageColorSpace];
return filter;
}
Your Objective-C class method
+ (ASHColorMonochromeFilter *) filterWithRed: (CGFloat) red green:(CGFloat)green blue:(CGFloat)blue
returns actually an instance of (a subclass) of CIFilter
. The same can be achieved
in Swift with a convenience initializer
extension CIFilter {
convenience init(red: CGFloat, green: CGFloat, blue: CGFloat) {
self.init(name:"CIColorMonochrome")
setDefaults()
let colour = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
setValue(colour, forKey: kCIInputColorKey)
}
}
which is called as
let filter = CIFilter(red: 0.1, green: 0.2, blue: 0.3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With