title: Using CIColorMatrix filter in Swift
body: <p>The following Swift func is supposed to tint a greyscale image 'greyImage' using specified 'tintColor':</p>
  
  <pre><code>import UIKit
  
  func colorizeImage(greyImage : UIImage, tintColor : UIColor) -&gt; UIImage? {
  
      let colorMatrixFilter = CIFilter(name: "CIColorMatrix")
  
      var r:CGFloat = 0
      var g:CGFloat = 0
      var b:CGFloat = 0
      var a:CGFloat = 0
      tintColor.getRed(&amp;r, green:&amp;g, blue:&amp;b, alpha:&amp;a)
  
      colorMatrixFilter.setDefaults()
      colorMatrixFilter.setValue(greyImage, forKey:"inputImage") //kCIInputImageKey)
      colorMatrixFilter.setValue(CIVector(x:r, y:0, z:0, w:0), forKey:"inputRVector")
      colorMatrixFilter.setValue(CIVector(x:0, y:g, z:0, w:0), forKey:"inputGVector")
      colorMatrixFilter.setValue(CIVector(x:0, y:0, z:b, w:0), forKey:"inputBVector")
      colorMatrixFilter.setValue(CIVector(x:0, y:0, z:0, w:a), forKey:"inputAVector")
  
      if let ciImage =  colorMatrixFilter.outputImage {
          return UIImage(CIImage: ciImage);
      }
  
      return nil;
  }
  </code></pre>
  
  <p>The colour is UIColor.orangeColor() (r=1,g=0.5,b=0,a=1), the greyscale image is OK since it is displayed all right when fed to ImageView.</p>
  
  <p>It looks like all the necessary keys are supplied and the key assignments go smoothly (BTW does the filter check the validity of keys, or takes everything?), however reading 'outputImage' property produces SIGABRT and the following console message:</p>
  
  <pre><code>2015-05-02 13:04:07.319 MyApp[436:8241] -[UIImage _internalRepresentation]: unrecognized selector sent to instance 0x7fd5b3ca82b0
  2015-05-02 13:04:07.629 MyApp[436:8241] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage _internalRepresentation]: unrecognized selector sent to instance 0x7fd5b3ca82b0'
  *** First throw call stack:
  (
      0   CoreFoundation                      0x00000001087abf35 __exceptionPreprocess + 165
      1   libobjc.A.dylib                     0x000000010a56cbb7 objc_exception_throw + 45
      2   CoreFoundation                      0x00000001087b304d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
      3   CoreFoundation                      0x000000010870b27c ___forwarding___ + 988
      4   CoreFoundation                      0x000000010870ae18 _CF_forwarding_prep_0 + 120
      5   CoreImage                           0x0000000108bd30fe -[CIColorMatrix outputImage] + 885
      6   MyApp                            0x00000001085c182d _TF8MyApp13colorizeImageFTCSo7UIImageCSo7UIColor_GSqS0__ + 4733
      7   MyApp                            0x00000001085c2b59 
  </code></pre>