【问题标题】:Received Memory Warning when calling "SetNeedDisplay" method continously in Objective - c programming在Objective-c编程中连续调用“SetNeedsDisplay”方法时收到内存警告
【发布时间】:2015-08-06 13:27:18
【问题描述】:

由于调用 setNeedsDisplay 方法而面临内存警告问题。以下是我完成的过程:

考虑我的应用程序是在非常大的 UIView(比如:MyExampleView)中使用CAShapeLayer 画线,比如(10000 * 8000)。在这个视图中,我使用panGesture 方法徒手画线。所以在平移手势方法中,每次都在“MyExampleView”中调用setNeedDisplay,因此它会收到内存警告并导致应用程序崩溃。

谁有建议可以告诉我们

添加代码:

-(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);
}

【问题讨论】:

  • 请分享你的代码

标签: ios objective-c memory warnings


【解决方案1】:

在每次调用drawRect: 时,您都在添加一个新的子层,而不会删除任何旧的子层。这很快就会使系统不堪重负。

无论如何,您都不应该在drawRect: 中添加层。如果在任何地方,您应该在handlePan: 中执行此操作。 drawRect: 用于重绘当前状态,而不是用于修改状态。 drawRect: 可以随时调用。如果它被重复调用,您当前的代码将为相同的 x,y 逐层添加。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多