stumped
stumped

Reputation: 3293

Setting colors for CGContextRef

I was just wondering how does setStroke know to set the stroke for context when context isn't mentioned at all in the setStroke method?

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 10);
[[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0] setStroke];

Btw how often do you programmatically draw your own objects?

Upvotes: 3

Views: 1961

Answers (2)

rob mayoff
rob mayoff

Reputation: 386018

This is the implementation of setStroke for your color:

- (void)setStroke {
    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.CGColor);
}

It uses the same function to get the context that you're using.

Upvotes: 3

Joe
Joe

Reputation: 57179

The same way you retrieved the context to set the line width is the same way the color can set the stroke. The reason is because there is only one current graphics context for the main thread retrieved by UIGraphicsGetCurrentContext(). You can push and pop different contexts but that function will always return the current one. Just remember that in iOS this function is not thread safe and should only be called from the main thread.

Upvotes: 2

Related Questions