Ios 在drawRect中优化动态CGContextDrawLinearGradient
我计算了一种新颜色,需要在视图的drawRect方法中以渐变方式渲染它 这会占用时间分析器中大量的执行时间,并且还会导致跳过或错过drawRect调用 在drawRect中优化渐变渲染的最佳方法是什么 我有以下代码Ios 在drawRect中优化动态CGContextDrawLinearGradient,ios,core-graphics,Ios,Core Graphics,我计算了一种新颜色,需要在视图的drawRect方法中以渐变方式渲染它 这会占用时间分析器中大量的执行时间,并且还会导致跳过或错过drawRect调用 在drawRect中优化渐变渲染的最佳方法是什么 我有以下代码 CGContextSaveGState(ctx); CGContextAddPath(ctx, path.CGPath); CGContextClip(ctx); // get the color components const CGFl
CGContextSaveGState(ctx);
CGContextAddPath(ctx, path.CGPath);
CGContextClip(ctx);
// get the color components
const CGFloat* components = CGColorGetComponents(fillColor.CGColor);
// create the gradient
CGGradientRef gradient;
CGColorSpaceRef colorspace;
size_t num_locations = 3;
CGFloat componentsArray[12] =
{
components[0], components[1], components[2], 1.0, // Start color
components[0], components[1], components[2], 1.0, // Middle color
components[0], components[1], components[2], 0.0 // End color
};
colorspace = CGColorSpaceCreateDeviceRGB();
gradient = CGGradientCreateWithColorComponents(colorspace, componentsArray, locations, num_locations);
CGContextDrawLinearGradient (ctx, gradient, gradientStartPoint, gradientEndPoint, 0);
CGGradientRelease(gradient);
CGContextRestoreGState(ctx);
你为什么经常打电话来?您是在设置渐变动画,还是在改变渐变以反映其他数据?这是使用数据的实时渲染。我正在使用UIBezierPath,我需要将这些路径存储在一个数组中,并在数组中重新命名所有路径,在每个drawRect调用中重新命名一个新路径。它运行平稳,没有梯度。梯度是唯一改变的东西吗?如果是这样的话,也许你可以使用
CAGradientLayer
和一些图层混合来达到你想要的效果。正如JonathanCichon所说,你可以尝试使用CAGradientLayer,如果你正在设置渐变变化的动画,这将非常好。我预计,如果您在每一帧手动更改颜色(BezierPaths更改),增益会小得多。以及它们内部的梯度。图层混合听起来很有趣。我来看看。如果我使用CAGRADENT层,代码会有多大的不同?还有,我该如何把它们放在贝塞尔路径中?