在iPad上绘制大量线条(CGContextBeginPath)
我正在尝试制作一个可以绘制很多内容的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请重新考虑您在那里所做的事情:
分配
一个GetColor
实例 - 您再也不会使用它。问问自己:从设计的角度来看这有意义吗?DrawLine< 的类对象
/code> (与下一行和
GetColor
类相同)。 这是非常非常错误的!请访问iOS 开发中心的内存管理编程指南并(再次)阅读前两节!
除此之外,重新评估您的设计:
GetColor
真的应该是一个类,以便您创建实例吗?在这种情况下,用于颜色插值的简单辅助函数不是更有意义吗?drawLayer:inContext:
方法的 CALayer 委托) )。如果您稍后需要更新这些行,您可以简单地(ab)使用 CALayer...在后一种情况下,您的问题就变成:
创建不透明 CALayer
a) 该颜色作为其背景颜色,
b) 宽度为 20 * sqrt(2),
c) 任意你想要的线宽高度,
d) 你的观点作为其起源和
e)
rotation
为 45。self.view
的层中。干杯
丹尼尔
Please reconsider what you are doing there:
alloc
an instance ofGetColor
— which you never use again. Ask yourself: Does that make any sense from a design point of view?DrawLine
(ditto that for the next line and theGetColor
-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:
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?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:
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.self.view
's layer.Cheers
Daniel
如果您的线条是静态的(稍后不移动,不动画等),就像它们看起来的那样,您也可以在一个视图中的单个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;)