Phased addition to UIView

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.

+7
source share
1 answer

BezierPaths are good because they are vectors, regardless of the resolution of your look, they will look beautiful. However, if you don’t need to resize your view, you don’t need to count all the time. Just calculate the last arc / line and draw it on the previous (off-screen) buffer, which should improve performance.

I remember how this was done, and the performance was not such a big problem that I got about 12 frames per second on the iPhone 3GS. Which is not so bad. (I didn’t use the bezier path exactly, I did some image processing using CGImageRefs and raw rgba buffers, which should be the most expensive part of this approach. Calculating just one BezierPath arc / line is very cheap)

+1
source

All Articles