iOS clear layer of UIView

14,386

Solution 1

Call CGContextClearRect with the current context and the rect to clear.


Based on your updated code, you aren't actually drawing anything. You're actually adding sub layers. So, to remove any previous line you need to remove the sub layer. You will need to come up with a way of finding the appropriate sub layer (maybe hold a property, maybe by tag) and then you can remove it just like you add it.


Don't perform the loop yourself, get the array to do it:

[viewToClear.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];

Solution 2

A much simpler way to remove all the sublayers of a UIView is to set its sublayers property to nil

viewToClear.layer.sublayers = nil;

Solution 3

I used something very similar to Erwan's response.. but it turned out I didn't need the 'layer' part. Here is what worked for me. Small but effective.

imageLayer.sublayers = nil;

Share:
14,386
Jasper Fioole
Author by

Jasper Fioole

Interested in programming in multiple languages.

Updated on June 27, 2022

Comments

  • Jasper Fioole
    Jasper Fioole almost 2 years

    I drew a few lines in the layer property of my UIView. But is there a method to clean up all the lines I drew?

    I want to clear everything that is drawn on layer of my view.

    I draw a line with the following code:

    - (void)drawLine :(UIView *)drawInView :(CGPoint)startPosition :(CGPoint)endPosition
    {
        //draw the line
        linePath = CGPathCreateMutable();
        lineShape = [CAShapeLayer layer];
    
        lineShape.lineWidth = 1.0f;
        lineShape.lineCap = kCALineCapRound;;
        lineShape.strokeColor = [[UIColor whiteColor] CGColor];
    
        CGPathMoveToPoint(linePath, NULL, startPosition.x, startPosition.y);
        CGPathAddLineToPoint(linePath, NULL, endPosition.x, endPosition.y);
    
        lineShape.path = linePath;
        CGPathRelease(linePath);
        [drawInView.layer addSublayer:lineShape];
    }
    

    I found some code to remove all the sublayers I drew.

    -(void)clearGraph :(UIView *)viewToClear
    {
        for (CALayer *layer in viewToClear.layer.sublayers) {
            [layer removeFromSuperlayer];
        }
    }
    

    But this will give an Exception:

    2013-08-28 21:10:18.877 ServerInfo[12861:3f03] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <CALayerArray: 0x1f86b3b0> was mutated while being enumerated.'
    *** First throw call stack:
    (0x336ef3e7 0x3b3ea963 0x336eeec1 0x13f7d 0x158bb 0x340082fb 0x336c4857 0x336c4503 0x336c3177 0x3363623d 0x336360c9 0x33f5a5c3 0x33ffdc45 0x15843 0x34007231 0x3b8370e1 0x3b836fa8)
    libc++abi.dylib: terminate called throwing an exception
    (lldb) 
    

    I call my drawline method inside a NSTread. (doParsing method to be specific).

    NSThread *myThread =[[NSThread alloc]initWithTarget:self selector:@selector(callTimer) object:nil];
        [myThread start];
    
    - (void)callTimer
    {
        NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(doParsing) userInfo:nil repeats:YES];
        [runLoop run];
    }