我正在尝试存档一个文本字段。其中文本字段文本是渐变色。我将其存档以将数据存储到我的核心数据库中。但是存档方法给我这个错误。 *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“在这种情况下仅支持 RGBA 或白色颜色空间。”
存档代码是 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/