issam issam - 4 months ago 16
Swift Question

CGColor not defined for the UIColor UIDeviceRGBColorSpace 1 1 0 1

application crash and i have this error in my Xcode console:

NSInvalidArgumentException', reason: '*** -CIColor not defined for the UIColor UIDeviceRGBColorSpace 1 1 0 1; need to first convert colorspace.


when I try to display the value of RGB color :

var color:UIColor
print("color \(color.CIColor.red) \(color.CIColor.green) \(color.CIColor.blue)")

Answer

From CIColor doc:

var CIColor: CIColor { get }

The Core Image color associated with the receiver. (read-only) This property throws an exception if the color object was not initialized with a Core Image color.

Use getRed method to get RGB:

var color = UIColor.redColor()
var fRed: CGFloat = 0
var fGreen: CGFloat = 0
var fBlue: CGFloat = 0
var fAlpha: CGFloat = 0
if color.getRed(&fRed, green: &fGreen, blue: &fBlue, alpha: &fAlpha) {
    print("color \(fRed)  \(fGreen)  \(fBlue)")
} else {
    print("error: color could not be converted")
}