【发布时间】:2018-03-19 09:38:14
【问题描述】:
我正在尝试存档一个文本字段。其中文本字段文本是渐变色。我将其存档以将数据存储到我的核心数据库中。但是存档方法给了我这个错误。 *** 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 以便将其存储到我的数据库中?
【问题讨论】:
-
不存档文本字段,您可以只存档文本吗?
-
我也这么认为。但我需要相同的渐变颜色。现在你可以说我也保存渐变颜色。但这是一个现有项目,许多用户使用它并且他们已经有数据在那里移动数据库。以前的版本只有文字锡颜色。但是这次我们需要给渐变。希望你理解@Flexicoder。
-
@Rumy 将 UIImage
func gradientImage转换为 NSData,保存到您的存储中,检索它,使用它。 -
@Rumy 您的渐变本质上只是您正在创建的图像的一种模式。我不明白为什么这会导致现有数据库模式出现问题,您只是添加一个 NSData 类型的新列。你的图片不是颜色!
-
@NSNoob 你想说将文本和渐变图像作为 NSData 存储到不同的字段中,当重新加载它们时,我们应该使用相同的方法对它们进行标记???
标签: ios swift core-data gradient