3

I am using UIBezierPath for drawing, and I have written the code on touch events and its working fine, but my curves are not smooth, when I am move my finger around and draw some curve, they are not smooth.

- (void)drawRect:(CGRect)rect
{    
    [[UIColor redColor] setStroke];
    for (UIBezierPath *_path in pathArray) 
    [_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}

#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    myPath=[[UIBezierPath alloc]init];
    myPath.lineWidth=5;
    myPath.lineCapStyle = kCGLineCapRound;
    myPath.flatness = 0.0;   

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    [myPath moveToPoint:[mytouch locationInView:self]];
    [pathArray addObject:myPath];

}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{   
    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    [myPath addLineToPoint:[mytouch locationInView:self]];

    [self setNeedsDisplay];    
}

Here is the image

enter image description here

In the above image if you see the letters a and d you will see that the curve is not smooth. What should I do, to get a smooth curve?

Christopher Oezbek
  • 17,629
  • 3
  • 48
  • 71
Ranjit
  • 4,566
  • 10
  • 53
  • 118

2 Answers2

4

Well you are using addLinePoint so obviously you get lines, not curves. You would be interested in other methods to give you smooth curves: addCurveToPoint or addQuadCurveToPoint. But as you can see from the API, besides the points you are actually drawing with your finger, you need also control points which do not come free with the drawing. Even Photoshop is asking you to move them around when doing curvature. In other words, making your hand drawing smooth "automagically" involves quite some mathematics. Google "smoothing hand drawn" and you will get at least these points to start with

Smoothing a hand-drawn free shape

Smoothing a hand-drawn curve

It is really not iOS specific at all.

Community
  • 1
  • 1
Pavel Zdenek
  • 6,883
  • 1
  • 18
  • 37
3

Just use this line. it will solve your problem myPath.miterLimit=-10;.

Change the value if you need to any thing it takes float value

The iOSDev
  • 5,048
  • 7
  • 39
  • 74
  • hey hi aalok, nice to see you again, hey your trick is not working, spikes are no more but the curve is not smooth, I am using your code which you had given link yesterday – Ranjit Jun 05 '12 at 12:58
  • so how to go about this, I am struggling from morning .:( – Ranjit Jun 05 '12 at 13:04
  • yes have to optimise it for smooth corves. Let's try and find out some way to do it. need some research for that. and this needs time – The iOSDev Jun 05 '12 at 13:06
  • i found that the change in linecapstyle is doing some work around but not very good smooth ness but some smoothness is achieved using `kCGLineJoinRound` try this out – The iOSDev Jun 05 '12 at 13:18
  • hey aalok you cannot use this for linecapstyle, its hould be used for lineJoinStyle – Ranjit Jun 05 '12 at 13:40
  • ok, anyways, hey you get to know any clues regarding the same please let me know – Ranjit Jun 05 '12 at 13:51
  • This answer should not be accepted, the other answer points to the real reason. You’re not going to get smooth curves if you’re simply adding line segments. – johnbakers Dec 01 '20 at 08:26