1

I am drawing many audio meters on a view and finding that drawRect can not keep up with the speed of the audio change. In practice only a very small part of the image changes at a time so I really only want to draw the incremental changes.

I have created a CGLayer and when the data changes I use CGContextBeginPath, CGContextMoveToPoint, CGContextAddLineToPoint and CGContextStrokePath to draw in the CGLayer.

In drawRect in the view I use CGContextDrawLayerAtPoint to display the layer.

When the data changes I draw just the difference by drawing a line over the top in the CGLayer. I had assumed it was like photoshop and the new data just draws over the old but I now believe that all the lines I have ever drawn remain present in the layer. Is that correct?

If so is there a way to remove lines from a CGLayer?

Ant
  • 1,668
  • 2
  • 18
  • 35

2 Answers2

0

What exactly do you mean by 'audio meter' show some snapshots of your intended designs. Shows us some code...

These are my suggestions-

1) Yes the new data just draws on top of CGLayer unless you release it CGLayerRelease(layer)

2) CGContextStrokePath is an expensive operation. You may want to create a generic line stroke and store them in UIImage. Then reuse the UIImage everytime your datachanges.

3) Simplest solution: use UIProgressView if you just want to show audio levels.

vforvendetta
  • 596
  • 7
  • 15
0

I now believe that all the lines I have ever drawn remain present in the layer. Is that correct?

Yes.

If so is there a way to remove lines from a CGLayer?

No. There is not. You would create a new layer. Generally, you create a layer for what is drawn repeatedly.

Your drawing may be able to be simplified by drawing rects rather than paths.

For some audio meters, dividing the meter into multiple pieces may help (you could use a CGLayer here). Similarly, you may be able to just draw rectangles selectively and/or clip drawing, images, and/or layers.

justin
  • 104,054
  • 14
  • 179
  • 226