Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subclassing CIFilter in Swift

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:

  1. Class 'ColorMonochromeFilter' does not implement its superclass's required members
  2. Must call a designated initializer of the superclass 'CIFilter'

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;
    }
like image 435
Ashley Mills Avatar asked Sep 30 '22 00:09

Ashley Mills


1 Answers

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)
like image 188
Martin R Avatar answered Nov 05 '22 01:11

Martin R