0

I'm trying to create sketch app that can draw shapes/path by finger.

What I've done so far is create UIBezierPath when touch start, and draw the path while finger moves.

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
CGPoint locationInDrawRect = [mytouch locationInView:self.drawingView];
[self.drawingView.currentPath addLineToPoint:locationInDrawRect];
[self.drawingView setNeedsDisplay]; 

}

when touch is done, save it to an array of UIBezierPath.

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.drawingView.pathsArray addObject:self.drawingView.currentPath];  //add latest current path to pathArray
    [self.drawingView clearCurrentPath];  //clear current path for next line
    [self.drawingView setNeedsDisplay];
}

in drawRect, draw current path and paths in array using for loop

- (void)drawRect:(CGRect)rect
{
        if(!self.currentPath.empty){
            [self.currentPath stroke];
        }

        for (UIBezierPath *path in self.pathsArray){
            [path stroke];
        }

}

This works for couple of paths objects, but it gets slow when the array holds more than 5 paths.

I tried to limit the area to render using setNeedsDisplayInRect: method.

[self.drawingView setNeedsDisplayInRect:rectToUpdateDraw]; 

and render path in array only when rect size is full canvas size, which is when touch is ended. But this draws weird shape line and also gets slow when there are many objects in the array.

I don't know how I can solve this problem and need some help.

Thank you!

STW
  • 44,917
  • 17
  • 105
  • 161
  • Have a look at the GLView example project on developers.apple.com, although it uses an OpenGL view rather than bezier paths. – danielbeard Aug 22 '12 at 01:22
  • 1
    if you have access to the latest WWDC sessions you should take a look at session 506 (Optimizing 2d graphics and animation performance). They do exactly what you did and optimize it step by step. –  Aug 22 '12 at 01:31
  • 1
    The biggest problem is updating all of the lines, and doing a full screen redraw. It's going to be even worse on an iPad 3. You won't be able to draw anything without a huge delay. You should create a CGBitmapContext and store your finished pixel data there. Then you only need to draw the bitmap context plus one stroke. Also, look at the video Sebrassi mentioned. – borrrden Aug 22 '12 at 01:36
  • danielbeard, Sebrassi, borrrden Thank you! Appreciate your help! – Kazuhito Ochiai Aug 22 '12 at 02:04

0 Answers0