How to use CgLayer for optimal drawing

I have created a simple drawing project,the code works fine, but I want to cache the drawing into CGlayer, because I read that its more efficient way in drawing . I have read through the documents, but not able to understand it properly. So friends, I request you to please help me in this regard.

Below is my code, I want to know how to use CgLayer in this

  • how to get the external ip in objective c
  • UITableView w/ paging & momentum
  • Generate a UUID on iOS from Swift
  • How to implement “group by values” in NSMutableArray?
  • Issue with Modal View Controllers and definesPresentationContext
  • Compare the text of two text fields
  • - (void)drawRect:(CGRect)rect
       CGContextRef context = UIGraphicsGetCurrentContext();
       if(myLayerRef == nil)
           myLayerRef = CGLayerCreateWithContext(context, self.bounds.size, NULL);
        CGContextRef layerContext = CGLayerGetContext(myLayerRef);
        CGContextDrawLayerAtPoint(context, CGPointZero, myLayerRef);   
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
        UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
        m_previousPoint2 = m_previousPoint1;
        m_previousPoint1 = [mytouch previousLocationInView:self];
        m_currentPoint = [mytouch locationInView:self];
        CGPoint mid1    = midPoint(m_previousPoint1, m_previousPoint2); 
        CGPoint mid2    = midPoint(m_currentPoint, m_previousPoint1);  
        testpath = CGPathCreateMutable();
        CGPathMoveToPoint(testpath, NULL, mid1.x, mid1.y);
        CGPathAddQuadCurveToPoint(testpath, NULL, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y);       
        CGContextRef context = UIGraphicsGetCurrentContext();
        context = CGLayerGetContext(myLayerRef);
        CGRect bounds = CGPathGetBoundingBox(testpath);   
        CGRect drawBox = bounds;
        //Pad our values so the bounding box respects our line width
        drawBox.origin.x        -= self.lineWidth * 2;
        drawBox.origin.y        -= self.lineWidth * 2;
        drawBox.size.width      += self.lineWidth * 4;
        drawBox.size.height     += self.lineWidth * 4;
       [self setNeedsDisplayInRect:drawBox];       
    - (void) drawingOperations
        CGContextRef context1 = CGLayerGetContext(myLayerRef);
        CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2); 
        CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);
        CGContextMoveToPoint(context1, mid1.x, mid1.y);
        CGContextAddQuadCurveToPoint(context1, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y); 
        CGContextSetLineCap(context1, kCGLineCapRound);
        CGContextSetLineWidth(context1, self.lineWidth);
        CGContextSetStrokeColorWithColor(context1, self.lineColor.CGColor);
        CGContextSetFlatness(context1, 2.0);
        CGContextSetAllowsAntialiasing(context1, true);


    2 Solutions Collect From Internet About “How to use CgLayer for optimal drawing”

    The link posted by @hfossli is now dead, but here is the archived content:

    CGLayer no longer recommended

    Posted by robnapier on Jul 13, 2012 in Book Updates

    I spend a lot of time in the labs at WWDC asking questions and talking with the developers. I sat down the Core Graphics engineers this time and asked them about one of my favorite underused tools: CGLayer, which I discuss at the end of Chapter 6. CGLayer sounds like a great idea: a drawing context optimized specifically for drawing on the screen, with hardware optimization. What could go wrong?

    I started to have doubts, though, that CGLayer was always a great win. What if your layers were too large to store in GPU textures? CGLayer is advertised for use as a “stamp” that you repeatedly draw. Moving data to and from the GPU is expensive. Maybe CGLayer doesn’t make sense unless you draw it a certain number of times. The docs give no guidance on this.

    So I asked the Core Graphics team “When should I be using CGLayer?”


    … ??? Never? But for stamping right?


    So we talked some more. It appears that CGLayer was one of those things that sounded great on paper, but just doesn’t always work in practice. Sometimes it’s faster. Sometimes its slower. There’s no easy rule for when it’s going to be faster. Over time it seems they’ve quietly abandoned it without actually deprecating it. I’ve asked that the docs be updated to match Apple’s current recommendation. The CGLayer Reference hasn’t been updated since 2006.

    The recommendation I received was to use CGBitmapContext or CALayer for stamping. For the specific example given on pages 131-132, CATextLayer would probably be the best tool. Remember that you can easily clone a CALayer using initWithLayer:. (John Mueller points out below that this isn’t actually supported.)

    Not optimal to use CGLayer anymore