Search code examples
objective-ciosdrawingcgpath

What algorithm should be used to decide which lines to redraw when the screen refreshes?


I made a simple iOS View based application which would let you draw and erase on the screen. I am using an NSMutableArray to store already drawn paths. and every time i switch the control to eraser from marker or vice versa , I add the current CGPath to the array and create a new one.

and every time in drawRect I redraw the array's paths with appropriate color depending on whether it was an eraser's path or marker's path

and draw the current one as well which is being draw as the touch moves.

Now I know that this is NOT AT ALL a good solution and would eat up a lot of RAM as the array size grows. My array will already be containing redundant paths that actually go over points that are already colored with same color and would be unnecessarily be eating processor's time for doin it again and the memory as well.

Can anyone refer a better algorithm to save on the resources ?


Solution

  • RAM wouldn't be your issue. Having solved this problem very recently, I can tell you that redrawing all the paths over again will start taxing the processor after a while. The way I solved this was to "rasterize" the paths to an image and just draw the image with the newest path on it. This seems to work well for any number of paths and scales well since the image size doesn't really change (size of the screen).

    Let me know if you need a specific code example.