【问题标题】:CIFilter CISmoothLinearGradient crashing when passing color in SwiftCIFilter CISmoothLinearGradient 在 Swift 中传递颜色时崩溃
【发布时间】:2019-01-31 20:11:35
【问题描述】:

我正在尝试在 iOS 应用程序中使用 CISmoothLinearGradient,无论我输入什么,它似乎都不喜欢第二个 inputColor,可能是因为 alpha 参数。

我尝试了其他几种设置 CIFilter 的方法,但我用它来确定问题出现的位置:

    let gradientFilter = CIFilter(name: "CISmoothLinearGradient")
    gradientFilter?.setDefaults()
    gradientFilter?.setValue([0, 0], forKey: "inputPoint0")
    gradientFilter?.setValue(inputPoint1Vector, forKey: "inputPoint1")
    gradientFilter?.setValue(UIColor.black, forKey: "inputColor0")
    gradientFilter?.setValue(UIColor(red: 1, green: 1, blue: 1, alpha: 1), forKey: "inputColor1") // crash

控制台状态: -[UICachedDeviceWhiteColor alpha]:无法识别的选择器发送到实例 0x1c4259830

最初我使用的是 UIColor.white,但它仍然显示 UICachedDeviceWhiteColor 错误。

【问题讨论】:

  • 这实际上是一个答案,但我会把它留给你作为学习练习。前两个键是CIVectors,后两个键是CIColorsdeveloper.apple.com/library/content/documentation/…
  • 呃.. 我错过了谢谢。我没有看到它们是 CIColors,而不是 UIColors。
  • 不要忘记CIVectors - 你的代码看起来不像。但我不确定这是否会修复您发布的错误,因为我原以为您会收到不同的错误。
  • 向量很好,inputColor0 工作正常,但是当我放入 CIColor 时,我无法让 inputColor1 工作。如果我输入 CIColor.black(或 inputColor1 的任何颜色),我的应用程序会立即崩溃。我希望我可以分享它给出的信息,但它似乎不再神奇地崩溃了!

标签: swift gradient uicolor cifilter


【解决方案1】:

CIColor 和 CIVector 将在这种情况下完成工作。

根据需要替换 RGB 和 x,y 的值

        var color1 = CIColor(red: 238/255, green: 97/255, blue: 35/255, alpha: 1)
        var color2 = CIColor(red: 246/255, green: 66/255, blue: 227/255, alpha: 1)

        let gradientFilter = CIFilter(name: "CISmoothLinearGradient")
        gradientFilter?.setDefaults()
        gradientFilter?.setValue(CIVector(x: 0, y: 0), forKey: "inputPoint0")
        gradientFilter?.setValue(CIVector(x: 200, y: 200), forKey: "inputPoint1")
        gradientFilter?.setValue(color1, forKey: "inputColor0")
        gradientFilter?.setValue(color2, forKey: "inputColor1")

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多