Rec*_*bot 1 cocoa-touch core-graphics subview drawrect ipad
我正在尝试制作一个非常吸引人的ipad应用程序,但我的意思是在舞台上有很多行(10.000+)
使用这个简单的forloop,我的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];
}
Run Code Online (Sandbox Code Playgroud)
这是我的"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);
}
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?如何在不崩溃iOS的情况下绘制这么多子视图?
非常感谢!!:)
请重新考虑你在那里做什么:
alloc是一个GetColor你永远不会再使用的实例.问问自己:从设计的角度来看,这有什么意义吗?DrawLine(同上,对于下一行和GetColor-class).这非常可怕!请访问iOS开发中心的内存管理编程指南,并再次阅读前两部分!
除此之外,重新评估您的设计:
GetColor真的是一个类,以便您创建实例?在这种情况下,用于颜色插值的简单辅助函数不是更有意义吗?drawLayer:inContext:方法的CALayer委托).如果您以后需要更新这些行,您可以简单地(ab)使用CALayer ...在后一种情况下,您的问题变为:
rotation45. self.view图层.干杯
丹尼尔