In my iOS application, I created a view that displays the path drawn by a finger using real-time UIBezierPath while drawing. Unfortunately, I have performance problems, because the path is long (several hundred vertices), and it becomes impractical to draw the whole path every time the view is updated.
I tried to split the path into many different segments, which allows me to draw only segments that are in the rectangle passed to me in drawRect: and this helps significantly, but the drawing is still slow; many segments are redrawn unnecessarily when sections are added to the path in an area of the screen that already contains parts of the path.
I am not deleting anything from the view, so I tried turning on clearsContextBeforeDrawing and calling only the last segment every time drawRect: is called drawRect: but there seems to be a lot of anecdotal evidence online that says this property does nothing, and I also couldn't make it work at all.
What is the best way to speed up your drawing?
From what I read, it seems like my best option is to draw in a (raster) raster context so that I can add it a bit and copy it into the graphic context displayed on the screen, but I also read that this often slows down as with distribution of context, and because of the conversion of RGBA to RGB, which is required for rendering the image on the screen.
It seems that I can do something with CALayer (either with a buffer window or without it) - I don’t understand how the layer on the layer works and whether it will bypass any of the problems that I have seen. The contents property takes the value CGImageRef ; can I make a CGImage and then update it step by step without redrawing everything? Also, is it possible to add CALayer content without redrawing all of this?
Any ideas would be appreciated.
Edit:. For a reward, give an example of proper initialization of the image buffer, draw a cubic path of bezier and copy it to the screen accordingly. Also pay attention to why using your method - I don't know if using CGBitmapContextCreate best way or if there is anything else that I should use.
Sophie alpert
source share