“压平”或“合并”石英 2D 画线

发布于 2024-10-12 04:48:18 字数 1981 浏览 2 评论 0原文

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

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

现在的问题:过了一会儿,它开始变得越来越慢,因为数组变得越来越慢>很长,所以我想我应该将坐标合并到平面图像或其他东西中,并清除数组以保持内存健康,但是我该怎么做?

这是我当前的工作流程:

  • 呼叫计时器

    [NSTimerchedTimerWithTimeInterval:0.1 target:selfselector:@selector(drawdrawdraw)userInfo:nil Repeats:YES];

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

    -(void)drawdrawdraw{ [自行设置需要显示]; }

  • 我的绘制矩形

    -(void)drawRect:(CGRect)rect{
        viewContext = UIGraphicsGetCurrentContext();
    
        int 计数 = [mijnArray_r_x 计数];
    
        浮点 r_x = (浮点)(随机() % 768);
        [mijnArray_r_x insertObject:[NSNumber numberWithFloat:r_x] atIndex:count];
    
        浮点 r_y = (浮点)(随机() % 1004);
        [mijnArray_r_y insertObject:[NSNumber numberWithFloat:r_y] atIndex:count];
    
        浮点 r_w = (浮点)(随机() % 100);
        [mijnArray_r_w insertObject:[NSNumber numberWithFloat:r_w] atIndex:count];
    
        浮点 r_a = (浮点)(随机() % 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 <= 计数; 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);
        }
    } }
    

I'm trying to figure out how you can flatten or merge alot of dynamicly drawn lines in Quartz 2D,

I'm drawing random lines over time on my stage, I add new line coordinates each time to an array and draw the array (with my drawRect and I push a new line in my array with a timer and a setNeedDisplay to acctually redraw all the previous lines plus the new one)

now the PROBLEM: after a while it starts to get slower and slower because the arrays are getting very long, so I though I should merge the coordinates into a flat image or something and clear the arrays to keep it memory healthy, but how do I do this?

This is my current workflow:

  • Call timer

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

  • Refresh the drawRect in my "drawdrawdraw" function

    -(void)drawdrawdraw{
    [self setNeedsDisplay];
    }

  • my 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);
        }
    } }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画▽骨i 2024-10-19 04:48:18

您可以使用 UIGraphicsBeginImageContext() 将线条绘制到 UIImage 中。这样,绘制所需的时间将是恒定的,因为在每次迭代中,您仅将缓存的图像绘制为位图以及添加的新行。

- (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();
}

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

You can draw your lines into a UIImage by using UIGraphicsBeginImageContext(). This way, the time needed to draw will be constant, because in each iteration you're only drawing the cached image as a bitmap and the new lines you add.

- (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();
}

This assumes that you have the property currentImage of type UIImage declared.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文