【问题标题】:How can the CALayerGradient conic be rendered?如何渲染 CALayerGradient 圆锥?
【发布时间】:2019-01-02 21:21:58
【问题描述】:

我正在尝试在 CGContext 中使用 CALayerGradient 渲染圆锥渐变

    let colors = [ UIColor(hue: 0, saturation: 1.0, brightness: 1.0, alpha: 1.0).cgColor,
                   UIColor(hue: 1/6, saturation: 1.0, brightness: 1.0, alpha: 1.0).cgColor,
                   UIColor(hue: 2/6, saturation: 1.0, brightness: 1.0, alpha: 1.0).cgColor,
                   UIColor(hue: 0.5, saturation: 1.0, brightness: 1.0, alpha: 1.0).cgColor,
                    UIColor(hue: 4/6, saturation: 1.0, brightness: 1.0, alpha: 1.0).cgColor,
                    UIColor(hue: 5/6, saturation: 1.0, brightness: 1.0, alpha: 1.0).cgColor,
                    UIColor(hue: 1.0, saturation: 1.0, brightness: 1.0, alpha: 1.0).cgColor ]

    let conicGradient = CAGradientLayer()
    conicGradient.type = .conic
    conicGradient.frame = bounds
    conicGradient.startPoint = CGPoint( x: 0.5, y: 0.5 )
    conicGradient.endPoint = CGPoint( x: 1, y: 1 )
    conicGradient.colors = colors as [Any]

    conicGradient.render( in: mContext ) // This doesn't work

    if let drawingContext = UIGraphicsGetCurrentContext()
    {
        drawingContext.draw( myContext.makeImage()!, in: bounds )
    }

上面,将类型更改为其他任何内容都可以。 另外,如果我不尝试渲染到 CGContext,而是将其添加为子层,它会起作用,唯一不起作用的是在 CGContext 中渲染的圆锥,这是我想做的事情。 那么是我遗漏了什么,还是没有实现圆锥的渲染?

【问题讨论】:

  • 什么是mContext?定义“不起作用”。千万不要在没有完全解释代码到底是什么方式不起作用的情况下使用这些词。
  • mContext 是我要渲染的 CGContext。不起作用,我的意思是没有绘制像素,只有清晰的图像,与其他图像不同,将像素渲染为有意的。
  • 我的目标是在“缓冲区”CGContext : mContext 中渲染渐变,然后最终在绘图函数中将其渲染到屏幕上。希望这能澄清我的目标

标签: ios swift gradient


【解决方案1】:

与其尝试渲染到任意上下文,不如尝试使用UIGraphicsPushContext(_:) 推送该上下文,绘制到当前上下文,然后使用UIGraphicsPopContext() 弹出该上下文。

【讨论】:

  • 我试过了,但由于我对 UIKit 一点也不熟悉,我可能做错了。我在 IUView 的绘图功能中做了什么:UIGraphicsPushContext( mContext ),然后是myConicGradient.render( in: mContext ),然后是UIGraphicsPopContext()。我终于得到了新的当前渲染上下文:if let drawingContext = UIGraphicsGetCurrentContext() 并在其中绘制了我的上下文drawingContext.draw( mContext, in: bounds ) 以将其绘制到屏幕上。结果仍然是清晰的图像
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
相关资源
最近更新 更多