Performance when frequently drawing CGPaths

后端 未结 5 1498
無奈伤痛
無奈伤痛 2021-02-01 08:16

I am working on an iOS App that visualizes data as a line-graph. The graph is drawn as a CGPath in a fullscreen custom UIView and contains at most 320

5条回答
  •  -上瘾入骨i
    2021-02-01 08:52

    This post here gives a nice explanation why that didn't help.

    It also explains why your drawRect: method is slow.

    You're creating a CGPath object every time you draw. You don't need to do that; you only need to create a new CGPath object every time you modify the set of points. Move the creation of the CGPath to a new method that you call only when the set of points changes, and keep the CGPath object around between calls to that method. Have drawRect: simply retrieve it.

    You already found that rendering is the most expensive thing you're doing, which is good: You can't make rendering faster, can you? Indeed, drawRect: should ideally do nothing but rendering, so your goal should be to drive the time spent rendering as close as possible to 100%—which means moving everything else, as much as possible, out of drawing code.

提交回复
热议问题