I'm trying to get a handle on Quartz 2D and looking at the online book Quartz 2D Graphics for Mac OS X Developers.
One very basic thing that is confusing me is the CGContext. For example, I can draw a simple "U" shape with the code below and use it in a CAShapeLayer
without referencing a CGContext
. Is the context implied/provided by default by the CAShapeLayer
?
I'm probably mixing up several iOS/OSX graphics APIs here so maybe someone can clarify where I am going wrong.
CGPoint pt1 = CGPointMake(100, 100);
CGPoint pt2 = CGPointMake(150, 100);
CGPoint pt3 = CGPointMake(150, 200);
CGPoint pt4 = CGPointMake(190, 200);
CGPoint pt5 = CGPointMake(190, 100);
CGPoint pt6 = CGPointMake(210, 100);
CGPoint pt7 = CGPointMake(210, 250);
CGPoint pt8 = CGPointMake(100, 250);
CGPoint pt9 = CGPointMake(100, 100);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, pt1.x,pt1.y);
CGPathAddLineToPoint(path, nil, pt2.x, pt2.y);
CGPathAddLineToPoint(path, nil, pt3.x, pt3.y);
CGPathAddLineToPoint(path, nil, pt4.x, pt4.y);
CGPathAddLineToPoint(path, nil, pt5.x, pt5.y);
CGPathAddLineToPoint(path, nil, pt6.x, pt6.y);
CGPathAddLineToPoint(path, nil, pt7.x, pt7.y);
CGPathAddLineToPoint(path, nil, pt8.x, pt8.y);
CGPathAddLineToPoint(path, nil, pt9.x, pt9.y);
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[self.view.layer addSublayer:shapeLayer];