On iOS, we can draw a line in drawRect using
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath (context);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, 100, 100);
CGContextStrokePath(context);
but we can also draw a rectangle if we remove the above code, and just use:
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)];
[path stroke];
Two related questions:
1) Why doesn’t UIBezierPath need to get or use the current context?
2) What if I have two context: one for screen, and one is a bitmap context, then how to tell which context to draw to for UIBezierPath? I thought it might be UIGraphicsSetCurrentContext but it doesn’t exist.
UIBezierPathdoes use a context. It uses the current UIKit graphics context. This is exactly the same thing that you’re already getting withUIGraphicsGetCurrentContext().If you want
UIBezierPathto use a different context you can useUIGraphicsPushContext(), but you have to remember to useUIGraphicsPopContext()when you’re done.