我有一个带有按钮的iOS绘图应用程序,让用户可以选择要绘制的颜色。由于我的画笔使用RGB值,但我的颜色常数是UIColors,我需要转换。我试过了
private var currentButton: UIButton? {
willSet{
currentButton?.layer.borderWidth = 0
}
didSet{
currentButton?.layer.borderWidth = 2
let index = (currentButton?.tag)! - 1
drawView.brush.blue = colors[index].ciColor.blue //Here is where I get the error
drawView.brush.green = colors[index].ciColor.green
drawView.brush.red = colors[index].ciColor.red
}
}
我在另一个StackOverflow问题中找到了。但是,当我尝试在第8行获取RGB值时,我收到此错误:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '*** -CIColor not defined for the UIColor UIExtendedSRGBColorSpace 1 1 1 1; need to first convert colorspace.'
它清楚地表明我需要转换色彩空间。但是CIColor.colorSpace
是get-only。我做错了什么?
答案 0 :(得分:6)
使用getRed(_:green:blue:alpha:)
:
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)