由于调用setNeedsDisplay方法而面临内存警告问题。以下是我所做的过程:
考虑我的应用是使用非常大的UIView中的CAShapeLayer
绘制线条(例如:MyExampleView
)说(10000 * 8000)
。在此视图中,我使用panGesture
方法徒手绘制线条。所以在平移手势方法中我每次都在“setNeedDisplay
”中调用MyExampleView
,因此它会收到内存警告并导致应用程序崩溃。
任何人都有建议请让我们知道
已添加代码:
-(void)handlePan:(UIPanGestureRecognizer *)gesture
{
exampleArea.currentX = someValue1;
exampleArea.currentY = someValue2;
// this someValue1 and someValue2 value keep on changing based on hand moving;
[exampleArea setNeedsDisplay]; // Here exampleArea - MyExampleArea class is subclass of UIView.
}
@implementation MyExampleArea
{
}
-(void)drawRect:(CGRect)rect
{
CGMutablePathRef linePath = nil;
linePath = CGPathCreateMutable();
if(!lineShape)
{
lineShape = [QlineShapes layer];
}
lineShape.lineWidth = 2.0f;
lineShape.lineCap = kCALineCapRound;;
lineShape.strokeColor = [[UIColor blueColor] CGColor];
CGPathMoveToPoint(linePath, NULL,100.0f,100.0f);
CGPathAddLineToPoint(linePath, NULL, _currentX, _currentY);
// _currentX, _currentY value will change
lineShape.path = linePath;
[self.layer addSublayer:lineShape];
CGPathRelease(linePath);
}
答案 0 :(得分:2)
在每次调用drawRect:
时,您都在添加新的子图层,而不删除任何旧的子图层。这很快就会破坏系统层。
在任何情况下,您都不应在drawRect:
中添加图层。如果有的话,你应该在handlePan:
中这样做。 drawRect:
用于重绘当前状态,而不是用于修改状态。可以随时调用drawRect:
。如果它被重复调用,那么您当前的代码将逐层添加相同的x,y。