gpt4 book ai didi

iphone - CGContextAddArc 真的那么慢吗(与用几条线绘制的圆相比)

转载 作者:行者123 更新时间:2023-12-03 19:16:00 25 4
gpt4 key购买 nike

各位,

在编写一些转盘和 slider 时(例如,像一个可以旋转的大音量按钮) - 我发现标准 CGContextAddArc() 的用法如下:

- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
CGContextSetLineWidth(ctx, radius * (KE-KR)+8);
CGContextSetStrokeColorWithColor(ctx,self.foregroundColor.CGColor);
.... more some colour/width/etc settings
...

CGContextAddArc(ctx, dx,dy,radius, 0, 2*M_PI, 0);

慢得令人难以置信。

在 iPad 上 - 带有少量实心/描边圆圈,在拖动过程中每秒少于大约 10 次干净的 [self setNeedsDisplay] 更新。一个非常快速的手绘圆圈(如下所示)的破解速度快了几个数量级。这同样适用于模拟器。

这是为什么呢。正常填充和各种渐变填充似乎都是这种情况。我做错了什么?

Dw。

// Stupid replacement for CGContectAddArc() which seems to be very slow.
//
void CGContextAddCirlce(CGContextRef ctx, float ox, float oy, float radius)
{
double len = 2 * M_PI * radius;
double step = 1.8 / len; // over the top :)

// translating/scaling would more efficient, etc..
//
float x = ox + radius;
float y = oy;

// stupid hack - should just do a quadrant and mirror twice.
//
CGContextMoveToPoint(ctx,x,y);
for(double a = step; a < 2.0 * M_PI -step; a += step) {
x = ox + radius * cos(a);
y = oy + radius * sin(a);
CGContextAddLineToPoint(ctx, x, y);
};

CGContextClosePath(ctx);
};

最佳答案

Quartz 2D 的矢量绘图操作可能很慢,这就是为什么最好只在需要时重绘。

就您的情况而言,我建议绘制一次音量按钮,然后使用旋转变换将您绘制按钮的 UIView 或 CALayer 进行变换。通过简单地移动、旋转或缩放 View ,您不会触发昂贵的重绘。内容已缓存为纹理,GPU 可以快速操作此光栅化内容并将其合成到其他 View 之上。

您会发现,以这种方式避免重绘将会大大提高性能。

关于iphone - CGContextAddArc 真的那么慢吗(与用几条线绘制的圆相比),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4180402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com