gpt4 book ai didi

ios - 如何提高 CGContextDrawPath/drawInContext : 的速度

转载 作者:可可西里 更新时间:2023-11-01 06:10:41 30 4
gpt4 key购买 nike

我在下面有以下代码,它基本上是 PieChart 的一个切片,其中有许多切片。每个切片都在它自己的 CALayer 中绘制,并使用 addSublayer: 添加到自定义 View 层。

问题是我在用户拖动手指时动态更新饼图(他们可以通过拖动来编辑饼图值)。它运行良好,但在重新绘制这些饼图“切片”时存在非常明显的滞后。我查看了 iOS Profiling 工具,它显示 >50% 的时间在 CGContextDrawPath() 中,因为每次用户移动一定程度时它都必须重新绘制饼图切片.

我的问题是,我可以做些什么来提高这段代码的速度?有什么我想念的吗?

另请注意,这段代码在 iPad 2 上运行良好(可接受的 fps 水平),但在 iPad 3 上运行起来很糟糕,根据我的估计,它慢了 2 倍。谁能解释一下?仅仅是视网膜显示屏吗?

-(void)drawInContext:(CGContextRef)ctx {

// Create the path
CGRect insetBounds = CGRectInset(self.bounds, self.strokeWidth, self.strokeWidth);
CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
CGFloat radius = MIN(insetBounds.size.width/2, insetBounds.size.height/2);

CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, center.x, center.y);

CGPoint p1 = CGPointMake(center.x + radius * cosf(self.startAngle), center.y + radius * sinf(self.startAngle));
CGContextAddLineToPoint(ctx, p1.x, p1.y);

int clockwise = self.startAngle > self.endAngle;
CGContextAddArc(ctx, center.x, center.y, radius, self.startAngle, self.endAngle, clockwise);

CGContextClosePath(ctx);

// Color it
CGContextSetFillColorWithColor(ctx, self.fillColor.CGColor);
CGContextSetStrokeColorWithColor(ctx, self.strokeColor.CGColor);
CGContextSetLineWidth(ctx, self.strokeWidth);

CGContextDrawPath(ctx, kCGPathFillStroke);
}

最佳答案

在 iPad 3 Retina 显示屏上绘制图形比在 iPad 2 非 Retina 显示屏上绘制要慢。

我记得 Apple 最初声称 iPad 3“图形处理速度比 iPad 2 快 4 倍”。事实并非如此。如果它在屏幕上渲染 Retina(多 4 倍的像素)应该至少与非 Retina iPad 2 相同的速度。

同样的问题发生在 Apple 发布 iPhone 4 时。iPhone 4 实际上比 3GS 和 3G 都慢。

CoreGraphics slower on iPhone4 than on 3G/3GS

要增加 fps,您需要减少 drawRect 需要绘制到的像素数。我建议将您的 CoreGraphics 相关内容绘制为半分辨率上下文。然后将该上下文作为位图并将其放大到全分辨率。但仅限于用户拖动时。

关于ios - 如何提高 CGContextDrawPath/drawInContext : 的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15387553/

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