【发布时间】: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 中渲染渐变,然后最终在绘图函数中将其渲染到屏幕上。希望这能澄清我的目标