On iOS, why UIBezierPath drawing doesn't require a context?

core-graphics, ios, iphone, uikit

Solution

`UIBezierPath` does use a context. It uses the current UIKit graphics context. This is exactly the same thing that you're already getting with `UIGraphicsGetCurrentContext()`.

If you want `UIBezierPath` to use a different context you can use `UIGraphicsPushContext()`, but you have to remember to use `UIGraphicsPopContext()` when you're done.

Problem

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.

Original source