“展平”或“合并”石英2D绘制线条

时间:2011-01-14 13:37:02

标签: objective-c quartz-2d lines

我正在试图弄清楚如何在Quartz 2D中展平或合并很多动态绘制的线条,

我在舞台上随着时间的推移绘制随机线条,每次在数组中添加新的线条坐标并绘制数组(使用我的drawRect并在数组中使用计时器和setNeedDisplay推送一个新行重新绘制所有前面的行加上新的行)

现在的问题:过了一段时间它开始越来越慢,因为数组得到很长,所以我虽然应该将坐标合并为平面图像或其他东西并清除数组为了保持记忆健康,但我该怎么做?

这是我目前的工作流程:

  • 通话计时器

    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(drawdrawdraw)userInfo:nil repeats:YES];

  • 刷新我的“drawdrawdraw”函数中的drawRect

    - (无效)drawdrawdraw {   [self setNeedsDisplay]; }

  • 我的drawRect

    -(void)drawRect:(CGRect)rect{
        viewContext = UIGraphicsGetCurrentContext();
    
        int count = [mijnArray_r_x count];
    
        float r_x = (float)(random() % 768);
        [mijnArray_r_x insertObject:[NSNumber numberWithFloat:r_x] atIndex:count];
    
        float r_y = (float)(random() % 1004);
        [mijnArray_r_y insertObject:[NSNumber numberWithFloat:r_y] atIndex:count];
    
        float r_w = (float)(random() % 100);
        [mijnArray_r_w insertObject:[NSNumber numberWithFloat:r_w] atIndex:count];
    
        float r_a = (float)(random() % 100);
        [mijnArray_r_a insertObject:[NSNumber numberWithFloat:r_a] atIndex:count];
    
    
        CGContextSetLineWidth(viewContext, 2.0);
        CGContextSetStrokeColorWithColor(viewContext, [UIColor blackColor].CGColor);
        for (int k = 0; k <= count; k++) {
    
            float temp_x = [[mijnArray_r_x objectAtIndex: k] floatValue];
            float temp_y = [[mijnArray_r_y objectAtIndex: k] floatValue];
            float temp_w = [[mijnArray_r_w objectAtIndex: k] floatValue];
            float temp_a = [[mijnArray_r_a objectAtIndex: k] floatValue];
    
            CGPoint pointpointpoint = CGPointMake(temp_x, temp_y);
            CGPoint pointpointpointpoint = CGPointMake(temp_w, temp_a);
            CGContextMoveToPoint(viewContext, pointpointpoint.x, pointpointpoint.y);
            CGContextAddLineToPoint(viewContext, pointpointpoint.x - pointpointpointpoint.x, pointpointpoint.y + pointpointpointpoint.y);
            CGContextStrokePath(viewContext);
        }
    } }
    

1 个答案:

答案 0 :(得分:0)

您可以使用UIImage将行划分为UIGraphicsBeginImageContext()。这样,绘制所需的时间将保持不变,因为在每次迭代中,您只是将缓存的图像绘制为位图和添加的新行。

- (void)drawRect:(CGRect)rect {
  [self.currentImage drawInRect:[self bounds]];
  UIGraphicsBeginImageContext([self bounds].size);
  CGContextRef cgContext = UIGraphicsGetCurrentContext();
  //Draw your lines into the cgContext here...
  self.currentImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
}

这假定您声明了currentImage类型的属性UIImage