Ios 如何归档文本颜色为渐变色的UItextfield?

Ios 如何归档文本颜色为渐变色的UItextfield?,ios,swift,core-data,gradient,Ios,Swift,Core Data,Gradient,我正在尝试存档一个文本字段。其中文本字段文本是渐变色。我将其存档以将数据存储到我的核心数据库中。但是存档方法给了我这个错误***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因是:“在这种情况下,仅支持RGBA或白色空格。”存档代码为let data=NSKeyedArchiver.archivedData(withRootObject:testerText)。viewdidload代码为 override func viewDidLo

我正在尝试存档一个文本字段。其中文本字段文本是渐变色。我将其存档以将数据存储到我的核心数据库中。但是存档方法给了我这个错误<代码>***由于未捕获的异常“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

        }

而不是归档文本字段,你可以只存档文本?我也这么认为。但我需要相同的渐变色。现在你可以说我也保存渐变色。但这是一个现有的项目,许多用户使用它,他们的移动数据库中已经有数据。以前的版本只有文本锡色。但这次我们需要提供渐变色。希望你取消重新启动@Flexicoder.@Rumy将UIImage
func gradientImage
转换为NSData,保存到存储器中,检索并使用它。@Rumy您的渐变实际上只是您正在创建的图像的一种模式。我不明白为什么这会导致现有数据库模式出现问题,您只是添加了一个NSData类型的新列。你的图像不是一种颜色@NSNoob您想说将文本和渐变图像作为NSData存储到不同的字段中,当重新加载它们时,我们应该使用相同的方法对它们进行边缘化吗???这并不能回答有关存档的问题。现在请尝试一下。我们必须只显示渐变文本,但我们只能得到文本,而不能得到颜色。我想归档一切都是OP想要的。