Touch draw in Quatz 2D/Core Graphics

Posted by OgreSwamp on Stack Overflow See other posts from Stack Overflow or by OgreSwamp
Published on 2010-06-01T17:38:28Z Indexed on 2010/06/01 17:43 UTC
Read the original article Hit count: 230

Hello, I'm trying to implement "hand draw tool". At the moment algorythm looks like that (I don't insert any code because methods are quite big, will try to explain an idea):

Drawing

  1. In touchesStarted: method I create NSMutableArray *pointsArray and add point into it. Call setNeedsDisplay: method.
  2. In touchesMoved: method I calculate points between last added point from the pointsArray and current point. Add all points to the pointsArray. Call setNeedsDisplay: method.
  3. In touchesFinished: event I calculate points between last added point from the array and current point. Set flag touchesWereFinished. Call setNeedsDisplay:.

Render:

drawRect: method checks is pointsArray != nil and is there any data in it. If there is - it starts to traw circles in each point of this array. If flag touchesWereFinished is set - save current context to the UIImage, release pointsArray, set it to nil and reset the flag.

There are a lot disadvantages of this method:

  • It is slow
  • It becomes extremely slow when user touches and move finger for long time. Array becomes enormous
  • "Lines" composed by circles are ugly

I would like to change my algorithm to make it bit faster and line smoother. In result I would like to have lines like on the picture at following URL (sorry, not enough reputation to insert an image): http://2.bp.blogspot.com/_r5VzEAUYXJ4/SrOYp8tJCPI/AAAAAAAAAMw/ZwDKXiHlhV0/s320/SketchBook+Mobile(4).png

Can you advice me, ho I can draw lines this way (smooth and slim on the edges)? I thought to draw circles with alpha gradient on the edges (to make lines smoother), but it will be extremely slowly IMHO.

Thanks for help

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c