我正在尝试归档一个文本字段,其中文本字段文本是渐变颜色。我将其归档以将数据存储到我的Core数据库中,但是archive方法给了我这个错误。 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only RGBA or White color spaces are supported in this situation.'
存档代码为let data = NSKeyedArchiver.archivedData(withRootObject: testerText)
。 viewdidload
代码是。
override func viewDidLoad() {
super.viewDidLoad()
testerText.textColor = UIColor(patternImage: gradientImage(size: testerText.frame.size, color1: CIColor(color: UIColor.green), color2: CIColor(color: UIColor.red), direction: .Left))
let data = NSKeyedArchiver.archivedData(withRootObject: testerText)
//print(toshow)
}
下面给出使文本颜色渐变的功能。
func gradientImage(size: CGSize, color1: CIColor, color2: CIColor, direction: GradiantDerection = .Up) -> UIImage {
let context = CIContext(options: nil)
let filter = CIFilter(name: "CILinearGradient")
var startVector: CIVector
var endVector: CIVector
filter!.setDefaults()
switch direction {
case .Up:
startVector = CIVector(x: size.width * 0.5, y: 0)
endVector = CIVector(x: size.width * 0.5, y: size.height)
case .Left:
startVector = CIVector(x: size.width, y: size.height * 0.5)
endVector = CIVector(x: 0, y: size.height * 0.5)
case .UpLeft:
startVector = CIVector(x: size.width, y: 0)
endVector = CIVector(x: 0, y: size.height)
case .UpRight:
startVector = CIVector(x: 0, y: 0)
endVector = CIVector(x: size.width, y: size.height)
}
filter!.setValue(startVector, forKey: "inputPoint0")
filter!.setValue(endVector, forKey: "inputPoint1")
filter!.setValue(color1, forKey: "inputColor0")
filter!.setValue(color2, forKey: "inputColor1")
let image = UIImage(cgImage: context.createCGImage(filter!.outputImage!, from: CGRect(x: 0, y: 0, width: size.width, height: size.height))!)
return image
}
现在,我想知道如何归档这些
textfield
以便将其存储到数据库中? 最佳答案
在TextField中尝试GradientText
override func viewDidLoad() {
super.viewDidLoad()
testerText.textColor = UIColor(patternImage: gradientImage(size: testerText.frame.size, color1: CIColor(color: UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)), color2: CIColor(color: UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.2))))
let data = NSKeyedArchiver.archivedData(withRootObject: testerText.text as Any)
}
func gradientImage(size: CGSize, color1: CIColor, color2: CIColor) -> UIImage {
let context = CIContext(options: nil)
let filter = CIFilter(name: "CILinearGradient")
var startVector: CIVector
var endVector: CIVector
filter!.setDefaults()
startVector = CIVector(x: size.width * 0.5, y: 0)
endVector = CIVector(x: size.width * 0.5, y: size.height * 0.8)
filter!.setValue(startVector, forKey: "inputPoint0")
filter!.setValue(endVector, forKey: "inputPoint1")
filter!.setValue(color1, forKey: "inputColor0")
filter!.setValue(color2, forKey: "inputColor1")
let image = UIImage(cgImage:
context.createCGImage(filter!.outputImage!, from: CGRect(x: 0, y: 0, width: size.width, height: size.height))!)
return image
}
关于ios - 如何归档文本颜色为渐变的UItextfield?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49359755/