改善drawRect的性能: [英] Improve performance of drawRect:

查看:61
本文介绍了改善drawRect的性能:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每隔0.1秒从一个带有NSTimer的网格中绘制一个单元.大小约为96x64 => 6144个单元/图像.如果我要绘制图像而不是(例如)绿色矩形,则速度要慢4倍!

I am drawing cells from a grid with a NSTimer every 0.1 Seconds. The size is about 96x64 => 6144 cells / images. If i am drawing images instead of (e.g.) green rectangles it is 4 times slower !

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIGraphicsPushContext(context);
    CGContextSetRGBFillColor(context, 0, 0, 0, 1);
    CGContextFillRect(context, CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height));
    int cellSize = self.bounds.size.width / WIDTH;
    double xOffset = 0;
    for (int i = 0; i < WIDTH;i++)
    {
        for (int j = 0; j < HEIGHT;j++)
        {                
                NSNumber *currentCell = [self.state.board objectAtIndex:(i*HEIGHT)+j];
                if (currentCell.intValue == 1)
                {
                   [image1 drawAtPoint:CGPointMake(xOffset + (cellSize * i),cellSize * j )];
                }
                else if (currentCell.intValue == 0){
                   [image2 drawAtPoint:CGPointMake(xOffset + (cellSize * i),cellSize * j )];
                }
        }
    }
    UIGraphicsPopContext();
}

如果我想在每个矩形中绘制png或jpg,有什么主意如何使它更快?图像已经按比例缩放到合适的大小.

Any idea how to makes this faster if i want to draw png or jpg in each rectangle? The images are already scaled to an appropriate size.

推荐答案

  • a)不要重绘视图范围之外的图像/矩形.

    • a) Don't redraw the images/rects that are outside the view's bounds.

      b)不要重新绘制dirtyRect外部的图像/矩形

      b) Don't redraw the images/rects that are outside the dirtyRect

      c)不要重绘自以前的更新.

      c) Don't redraw the images/rects that haven't changes since the previous update.

      d)使用图层预渲染图像,因此您无需渲染他们在绘图时.

      d) Use a layer to prerender the images, so you don't need to render them at drawing time.

      这篇关于改善drawRect的性能:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆