在iPad上绘制大量线条(CGContextBeginPath)

发布于 2024-10-10 15:23:54 字数 1288 浏览 0 评论 0原文

我正在尝试制作一个可以绘制很多内容的 ipad 应用程序,但我的意思是

使用这个简单的 forloop 在舞台上绘制很多线条(10.000+),我的 ipad 在 40~60 秒后崩溃(没有显示结果)

for ( int i = 0; i < 10000; i++ )
    {
        int r_x = rand() % 750;
        int r_y = rand() % 1000;
        CGPoint pointpoint = CGPointMake(r_x, r_y);
        UIColor *st = [[GetColor alloc] getPixelColorAtLocation:pointpoint];
        DrawLine *drawview = [[DrawLine alloc]initWithFrame:CGRectMake(r_x, r_y, 20, 20) selectedcolor:st];
        [self.view addSubview:drawview];
        [drawview release];
        [DrawLine release];
        [GetColor release];
    }

,这是我的“DrawLine” ” 班级:

- (id)initWithFrame:(CGRect)frame selectedcolor:colors{
    if ((self = [super initWithFrame:frame])) {
        selectedcolor_t = colors;
        self.backgroundColor = [UIColor clearColor];
    } 
    return self;
}

- (void)drawRect:(CGRect)frame{
    CGContextRef c = UIGraphicsGetCurrentContext();
    float* colors = CGColorGetComponents(selectedcolor_t.CGColor);
    CGContextSetStrokeColor(c, colors);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 0.0f, 0.0f);
    CGContextAddLineToPoint(c, 20.0f, 20.0f);
    CGContextStrokePath(c);

}

我该如何解决这个问题?如何绘制这么多子视图而不导致 iOS 崩溃?

非常感谢!! :)

I'm trying to make an ipad application that draws alot, but I really mean alot of lines on stage (10.000+)

using this simple forloop, my ipad crashes after 40~60 seconds (without showing result)

for ( int i = 0; i < 10000; i++ )
    {
        int r_x = rand() % 750;
        int r_y = rand() % 1000;
        CGPoint pointpoint = CGPointMake(r_x, r_y);
        UIColor *st = [[GetColor alloc] getPixelColorAtLocation:pointpoint];
        DrawLine *drawview = [[DrawLine alloc]initWithFrame:CGRectMake(r_x, r_y, 20, 20) selectedcolor:st];
        [self.view addSubview:drawview];
        [drawview release];
        [DrawLine release];
        [GetColor release];
    }

and this is my "DrawLine" class:

- (id)initWithFrame:(CGRect)frame selectedcolor:colors{
    if ((self = [super initWithFrame:frame])) {
        selectedcolor_t = colors;
        self.backgroundColor = [UIColor clearColor];
    } 
    return self;
}

- (void)drawRect:(CGRect)frame{
    CGContextRef c = UIGraphicsGetCurrentContext();
    float* colors = CGColorGetComponents(selectedcolor_t.CGColor);
    CGContextSetStrokeColor(c, colors);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 0.0f, 0.0f);
    CGContextAddLineToPoint(c, 20.0f, 20.0f);
    CGContextStrokePath(c);

}

how can I solve this problem? How can I draw this much subviews without crashing the iOS?

thanks so much!! :)

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

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

发布评论

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

评论(2

风流物 2024-10-17 15:23:55

请重新考虑您在那里所做的事情:

  1. 在循环的第 4 行中,您分配一个 GetColor 实例 - 您再也不会使用它。问问自己:从设计的角度来看这有意义吗?
  2. 在同一行中,如果您不违反 Cocoa 的命名约定,您将创建一个永远不会释放的 UIColor...
  3. 然后在第 8 行中,您释放 DrawLine< 的类对象/code> (与下一行和 GetColor 类相同)。 这是非常非常错误的!

请访问iOS 开发中心的内存管理编程指南并(再次)阅读前两节!

除此之外,重新评估您的设计:

  • GetColor 真的应该是一个类,以便您创建实例吗?在这种情况下,用于颜色插值的简单辅助函数不是更有意义吗?
  • 如果它应该是一个类,为什么不在循环之外仅创建它的一个实例并简单地重复查询它的颜色呢?
  • 你真的需要 UIView 的子类来绘制一条单一的直的、实心的、单色的线吗?如果不需要更新线条,您应该(如 Richard 和 nacho4d 建议的那样)将所有线条绘制在一个对象中(例如,通过自定义 UIView 或通过实现 drawLayer:inContext: 方法的 CALayer 委托) )。如果您稍后需要更新这些行,您可以简单地(ab)使用 CALayer...

在后一种情况下,您的问题就变成:

  1. 计算随机坐标。
  2. 计算你的颜色。
  3. 使用
    创建不透明 CALayer
    a) 该颜色作为其背景颜色,
    b) 宽度为 20 * sqrt(2),
    c) 任意你想要的线宽高度,
    d) 你的观点作为其起源和
    e) rotation 为 45。
  4. 将该层作为子层添加到 self.view 的层中。

干杯
丹尼尔

Please reconsider what you are doing there:

  1. In line 4 of your loop, you alloc an instance of GetColor — which you never use again. Ask yourself: Does that make any sense from a design point of view?
  2. In that same line, if you don't violate Cocoa's naming-conventions, you create a UIColor that is never released...
  3. Then in line 8 you release the class-object of DrawLine (ditto that for the next line and the GetColor-class). This is terribly, horribly wrong!

Please visit the Memory Management Programming Guide at the iOS Dev-Center and read the first two sections (again)!

Besides that, re-evaluate your design:

  • Should GetColor really be a class, so that you create instances? Wouldn't a simple helper-function for color interpolation make more sense in this context?
  • If it should be a class, why not create just one instance of it outside of the loop and simply query it repeatedly for the colors?
  • Do you really need a subclass of UIView to draw a single straight, solid, single-colored line? If the lines need not be updated, you should (as Richard and nacho4d suggested) draw all of them in one object (e.g. by a custom UIView or by a delegate of CALayer implementing the drawLayer:inContext: method). If you need to update those lines later, you could simply (ab)use CALayer...

In the latter case, your problem then becomes:

  1. Calculate your random coordinates.
  2. Calculate your color.
  3. Create an opaque CALayer with
    a) that color as its backgroundColor,
    b) a width of 20 * sqrt(2),
    c) a height of whatever-you-want-to-be-the-width-of-that-line,
    d) your point as its origin and
    e) a rotation of 45.
  4. Add that layer as a sublayer to self.view's layer.

Cheers
Daniel

安静被遗忘 2024-10-17 15:23:55

如果您的线条是静态的(稍后不移动,不动画等),就像它们看起来的那样,您也可以在一个视图中的单个drawRect:中绘制所有线条,而无需创建 1000 个 CALayers。
我无法判断这是否比绘制 1000 CALayers 更快(因为 CoreAnimation 是硬件加速的,而 CoreGraphics 不是),但它肯定更轻,因为所有线条都会在单个位图中展平。 (这是你的视图的上下文)

只需将你的 for 循环移到你的 drawRect: 中并遵循 danyowde 建议。(你只需要一个颜色对象或一个辅助函数,但不需要每次迭代创建一种颜色)

祝你好运,希望它有帮助; )

If your lines are static (not moving later, not animating, etc) , as they seem to be, you could also draw all the lines in a single drawRect: in one view without creating 1000 of CALayers.
I can't tell if this is faster than drawing 1000 CALayers (because CoreAnimation is hardware accelerated and CoreGraphics is not) but it's surely lighter since all the lines will be flattened in a single bitmap. (which is the context of your view)

Just move your for loop inside your drawRect: and follow danyowde advices.( you just need one color object or a helper function but not to create a color each iteration)

Good luck, Hope it helps;)

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