Reputation: 4270
I've been using the following code to draw the user input in my basic paint application:
CGContextRef drawSpace = CGLayerGetContext(currentLayer);
CGContextBeginPath(drawSpace);
CGContextMoveToPoint(drawSpace, [[self.currentStroke objectAtIndex:0]pointValue].x, [[self.currentStroke objectAtIndex:0]pointValue].y);
for (NSValue *v in self.currentStroke) {
CGContextAddLineToPoint(drawSpace, [v pointValue].x ,[v pointValue].y);
/* CGContextMoveToPoint(drawSpace, [v pointValue].x ,[v pointValue].y);
The link I got only showed this called when index = 0, so I dummied it out. Left in,
it produces the original jagged image behavior */
}
CGContextClosePath(drawSpace);
CGContextSetStrokeColorWithColor(drawSpace, [[NSColor blackColor]CGColor]);
CGContextSetLineWidth(drawSpace, self.BrushSize); //Sets the brush size
CGContextStrokePath(drawSpace); //Strokes the path to the layer
Which works fine at small line widths, but at large sizes the image comes out like the drawing below. I know it has to do with the angles, but I have no idea how to rectify this problem. Does anyone know how I can get a clean stroke at any size?
Upvotes: 1
Views: 69
Reputation: 1288
What you see in your image is the result of Core Graphics drawing single line segments one after another. So, line corners are not drawn of course and on large penWidths it shows like that.
What you should do is stroke the line only after finalising drawing all segments. In that case the runtime will take care of corners for you.
See also this question for more info on how to implement.
Upvotes: 1