0

I am working on a graphing application which takes in a stream of data and displays it. It turns out that drawing a lot of line segments in UIView (Core Graphics) is a very slow. I'm trying to speed it up by only drawing a portion of the screen that has new data.

I currently use this function to redraw the screen (using Xamarin/monotouch)

        NSTimer.CreateRepeatingScheduledTimer (TimeSpan.FromSeconds (0.1), delegate {
            pane.SetNeedsDisplay ();
        });

where pane is my UIView object. The pane object knows itself what part of the screen needs to be redrawn. I think there's another method that specifies a region of the screen to redraw - is that the right way to go? Or some other solution?

Also, the code draws a set of grids. These never change. Is there a way to save them to some layer or something so that I can just add it to the background?

The alternative is to go with openGL though drawing lines doesn't look as good and I'll have to implement a lot of extra code to make it look right.

Thanks!

reza
  • 1,329
  • 2
  • 22
  • 37

2 Answers2

2

For the graph data, you should be using CoreGraphics directly to render the lines in your UIView subclass' drawRect: method. Make sure that method only redraws the data that overlaps with the CGRect argument. The method setNeedsDisplay marks the entire UIView as needing a redraw, so instead of using that when your graph data changes, you should use the UIView's method setNeedsDisplayInRect: to indicate that there's new data in that region of the UIView that will call drawRect: to be called, which will only redraw that portion (if you've implemented it right).

Regarding the background grid, you can render the grid to a UIImage then display that UIImage behind your view that draws the graph. You'll want to look at the documentation for UIGraphicsBeginImageContext, CALayer renderInContext:, UIGraphicsGetImageFromCurrentImageContext, and UIGraphicsEndImageContext.

Idles
  • 1,131
  • 5
  • 9
  • would I need to somehow specify that my UIView has a transparent background? – reza Jun 20 '13 at 21:20
  • Yes, for the UIImageView to show through. You should be able to do this by calling `[super drawInRect:rect];` from your `drawInRect:` method, and making sure to set the background color of the UIView to `[UIColor clearColor];` when it's initialized. You may also need to set the opaque BOOL property to false. – Idles Jun 20 '13 at 21:23
1

You can use this variant:

void SetNeedsDisplayInRect (RectangleF rect);

That will only request that the specified region be redrawn.

miguel.de.icaza
  • 32,654
  • 6
  • 58
  • 76