【问题标题】:CoreGraphics performance with gradients渐变的 CoreGraphics 性能
【发布时间】:2012-12-13 15:24:54
【问题描述】:

屏幕上有多个正在移动的形状。
每个形状都可以根据其位置而变化,并使用 CoreGraphics 填充渐变。

它的性能很差 - 在 iPad3 上约为 9fps。

有什么加快速度的技巧吗?有一个预渲染的位图并改变它的形状会有所帮助吗?

我正在重新计算形状(三角形)并重新绘制每个动作:

- (void) drawGradientShapeInRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGFloat start_x = self.frame.size.width / 2.0;
    CGFloat start_y = self.frame.size.height;

    CGFloat end_x = radius_ * cos(angleInRad_) + start_x;
    CGFloat end_y = MAX(radius_ * sin(angleInRad_) + start_y, self.bounds.size.height - constrainedHeight_);

    // Create Lines
    CGPoint startPt = CGPointMake(start_x, start_y);

    CGContextMoveToPoint(context, self.center.x, self.center.y);
    CGPoint addLines[] =
    {
        startPt,
        CGPointMake(end_x, end_y),
        CGPointMake(end_x, start_y),
        startPt
    };

    CGContextAddLines(context, addLines, sizeof(addLines)/sizeof(addLines[0]));

    // Setup Gradient
    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
    NSArray *colors = nil;
    if(start_x > end_x)
    {
        colors = [NSArray arrayWithObjects:
                  (id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0].CGColor,
                  (id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f].CGColor,
                  nil];
    }
    else
    {
        colors = [NSArray arrayWithObjects:
                  (id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f].CGColor,
                  (id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0].CGColor,
                  nil];
    }

    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
    CGColorSpaceRelease(space);
    CGColorSpaceRelease(rgb);

    CGPoint startGradientPt = CGPointMake((start_x < end_x) ? start_x : end_x, 0);
    CGPoint endGradientPt   = CGPointMake((start_x > end_x) ? start_x : end_x, 0);

    CGContextSaveGState(context);
    CGContextClip(context);
    CGContextDrawLinearGradient(context,
                                gradient,
                                startGradientPt,
                                endGradientPt,
                                0);
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
}

【问题讨论】:

  • 你是如何绘制形状的?你是如何移动它们的?等等等等(我们需要更多细节)
  • 已更新 - 稍后会投放更多广告。

标签: ios ipad core-animation core-graphics


【解决方案1】:

最终创建了可以缩小和成形的 PNG。
帧率立即跃升至 40fps - 问题解决了。

【讨论】:

  • 如何拉伸渐变而不让它看起来很糟糕?
  • 在 Photoshop 中制作了一个半透明的 500x500 渐变三角形 - 所有形状的质量都接近视网膜屏幕上 CG 中绘制的质量。在非视网膜上,边缘有点“硬”
  • 好的,所以你正常收缩它,而不是拉伸它。有道理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 2019-06-30
相关资源
最近更新 更多