1

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?

My drawing

PopKernel
  • 4,110
  • 5
  • 29
  • 51

1 Answers1

1

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.

Community
  • 1
  • 1
insys
  • 1,288
  • 13
  • 26
  • Sounds good, but what should I display while the user is drawing with the mouse? – PopKernel Feb 15 '14 at 16:09
  • Well, if I were you I would store the points that the user drew in a single stroke and re-draw the whole line every time the mouse moves. That should give you the result you wish. – insys Feb 15 '14 at 16:22
  • I tried that, but it's not working. Maybe I'm doing it wrong? I'm going to update the question… – PopKernel Feb 15 '14 at 23:03
  • Don't update your existing question and code. The new question you are asking is different, so you'll confuse people. Open a new question please. – insys Feb 16 '14 at 12:09
  • Oops. I've made a mess of things. Sorry, I'm a little new to Stack Overflow. I'll put things back best I can. – PopKernel Feb 16 '14 at 21:13