我正在编写一个用户可以在uiview上绘制的应用程序。如果uiview是正常尺寸(比如1024 x 720),它的工作原理很完美。但是,如果我将其添加到uiscrollview中并且该维度为1024 x 3000,则会变得非常慢。此外,如果高度为10000,应用程序当场崩溃。我想知道怎么做。
- (void) drawRect: (CGRect) rect
{
NSLog(@"drawrect here 1");
if (self.arrayStrokes)
{
int arraynum = 0;
// each iteration draw a stroke
// line segments within a single stroke (path) has the same color and line width
for (NSDictionary *dictStroke in self.arrayStrokes)
{
NSArray *arrayPointsInstroke = [dictStroke objectForKey:@"points"];
UIColor *color = [dictStroke objectForKey:@"color"];
float size = [[dictStroke objectForKey:@"size"] floatValue];
[color set]; // equivalent to both setFill and setStroke
// // won't draw a line which is too short
// if (arrayPointsInstroke.count < 3) {
// arraynum++;
// continue; // if continue is executed, the program jumps to the next dictStroke
// }
// draw the stroke, line by line, with rounded joints
UIBezierPath* pathLines = [UIBezierPath bezierPath];
CGPoint pointStart = CGPointFromString([arrayPointsInstroke objectAtIndex:0]);
[pathLines moveToPoint:pointStart];
for (int i = 0; i < (arrayPointsInstroke.count - 1); i++)
{
CGPoint pointNext = CGPointFromString([arrayPointsInstroke objectAtIndex:i+1]);
[pathLines addLineToPoint:pointNext];
}
pathLines.lineWidth = size;
pathLines.lineJoinStyle = kCGLineJoinRound;
pathLines.lineCapStyle = kCGLineCapRound;
[pathLines stroke];
arraynum++;
}
}
}
答案 0 :(得分:1)
在提供的代码示例中没有任何明显的东西会导致真正大视图的问题。我做了一个快速测试,我正在绘制一个1024 x 10,000的视图而没有任何事件。我通过仪器对此进行了演绎,结果非常令人满意:
您应该通过仪器运行您的应用程序,并且(a)确保您没有泄漏; (b)查看分配并确保其水平。如果它正在增长,你应该确定导致这种增长的原因(在分配窗口中通过选项 -dragging或执行快照)。大量问题可能会导致分配增长(某些Core Foundation对象失败CGRelease
,上下文开头/结尾不匹配等等),所以我不敢猜测您的问题可能是什么。我没有在您提供的代码示例中看到任何明显的内容。我只建议通过静态分析器运行代码(“产品”菜单中的“分析”),看看它是否识别出任何内容。
现在,可能有问题的是使用UIImage
和renderInContext
将其保存为UIGraphicsGetImageFromCurrentImageContext()
。你没有说过你正在这样做,但尝试创建/保存图像会消耗这些尺寸的惊人数量的内存。
如果您不熟悉使用Instruments跟踪内存问题,我建议观看WWDC 2012视频iOS App Performance: Memory。这是关于这个主题的一个很好的入门书。
答案 1 :(得分:0)
如果手机不允许你创建一个大于有理由的CGrect,如果手机内存不足你可以做的很少,试着用不同的解决方案解决你的问题,但记忆手机有限,你不能滥用。
答案 2 :(得分:0)
这是因为UIView会在您实现-drawRect:
函数时分配内存。
这就是答案 Drawing a grid in UIScrollView's subview allocates huge memory