6

My application is able to do some simple figure's drawing (until I get a more stable code, I am sticking with only one figure) and it's also able to re-size them. The code I use to create a UIView is the following :

- (void)drawRect:(CGRect)rect
{    
    CGContextRef context = UIGraphicsGetCurrentContext();

    [self setContextFillColor:context];
    [self setContextStrokeColor:context];
    [self setLineWidth:context];

    CGFloat lineThickness = [self lineWidth] ;
    CGFloat halfLineThickess = lineThickness / 2.0f;

    CGContextAddRect(context, CGRectMake(halfLineThickess,halfLineThickess, rect.size.width - lineThickness, rect.size.height - lineThickness));

    CGContextDrawPath(context, kCGPathEOFillStroke);
}

Which gives me, with an input size of (100.0f,100.0f), this:

enter image description here

This figure is inside a "container" UIView which in turn is inside an UIScrollView. My problem with this is when I am re-sizing my figure and I reach sizes of around 1000*1000 (my "container" is 20,000*20,000), I start receiving memory warnings and the application finally gives up. So my questions are:

1) Should I consider putting a maximum size for the UIView?

2) How could I use the instruments to track this kind of problems, and see where the problem is coming (or where is the heavy lifting being done).

3) Should I use some kind of caching like this?

4) Are there some general best practices to consider, when drawing an UIView?

5) Should I consider CALayer, although I need to listen to touches?

The re-sizing of the UIView is based mostly on this.

4

2 回答 2

1

1)我认为如果您使用正确的方法,大小不是问题。

2) 你应该使用 Instruments 看看关于跟踪内存问题的 WWDC 视频

3,4) 我认为- (void)drawRect:(CGRect)rect这不是一个好习惯。如果你会看图纸会更好CALayer。您可以使用自定义CGMutablePathRef进行绘图。

所以 UIView 适合处理触摸但不适合绘制形状。

于 2012-10-17T17:30:32.327 回答
1

您不应该创建这么大的 UIView。它太大了。此类问题正是CATiledLayer旨在解决的问题。基本上,您需要跟踪形状的边界框,并使用这些边界框来确定哪些图块与哪些形状相交。当系统要求一个给定的图块时,您会绘制与其相交的所有形状。对于非常大和复杂的形状,您可以通过拆分形状来进一步优化,但通常不需要这样做。

于 2012-10-18T15:49:18.043 回答