下面的 Swift 函数应该使用指定的 'tintColor' 对灰度图像 'greyImage' 进行着色:
颜色为UIColor.orangeColor() (r=1,g=0.5,b=0,a=1),灰度图像可以正常显示,因为它被提供给ImageView时可以正常显示。看起来所有必要的键都已经提供并且键分配顺利(顺便问一下,滤镜是否检查键的有效性,还是接受任何键?),但是读取“outputImage”属性会产生SIGABRT和以下控制台消息:
import UIKit
func colorizeImage(greyImage : UIImage, tintColor : UIColor) -> UIImage? {
let colorMatrixFilter = CIFilter(name: "CIColorMatrix")
var r:CGFloat = 0
var g:CGFloat = 0
var b:CGFloat = 0
var a:CGFloat = 0
tintColor.getRed(&r, green:&g, blue:&b, alpha:&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;
}
颜色为UIColor.orangeColor() (r=1,g=0.5,b=0,a=1),灰度图像可以正常显示,因为它被提供给ImageView时可以正常显示。看起来所有必要的键都已经提供并且键分配顺利(顺便问一下,滤镜是否检查键的有效性,还是接受任何键?),但是读取“outputImage”属性会产生SIGABRT和以下控制台消息:
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