Improving Drawing Performance

Drawing is a relatively expensive operation on any platform, and optimizing your drawing code should always be an important step in your development process. Table A-1 lists several tips for ensuring that your drawing code is as optimal as possible. In addition to these tips, you should always use the available performance tools to test your code and remove hotspots and redundancies.

Table A-1  Tips for improving drawing performance



Draw minimally

During each update cycle, you should update only the portions of your view that actually changed. If you are using the drawRect: method of UIView to do your drawing, use the update rectangle passed to that method to limit the scope of your drawing. For OpenGL drawing, you must track updates yourself.

Call setNeedsDisplay: judiciously

If you are calling setNeedsDisplay:, always spend the time to calculate the actual area that you need to redraw. Don’t just pass a rectangle containing the entire view.

Also, don’t call setNeedsDisplay: unless you actually need to redraw content. If the content hasn’t actually changed, don’t redraw it.

Mark opaque views as such

Compositing a view whose contents are opaque requires much less effort than compositing one that is partially transparent. To make a view opaque, the contents of the view must not contain any transparency and the opaque property of the view must be set to YES.

Reuse table cells and views during scrolling

Creating new views during scrolling should be avoided at all costs. Taking the time to create new views reduces the amount of time available for updating the screen, which leads to uneven scrolling behavior.

Reuse paths by modifying the current transformation matrix

By modifying the current transformation matrix, you can use a single path to draw content on different parts of the screen. For details, see Using Coordinate Transforms to Improve Drawing Performance.

Avoid clearing the previous content during scrolling

By default, UIKit clears a view’s current context buffer prior to calling its drawRect: method to update that same area. If you are responding to scrolling events in your view, clearing this region repeatedly during scrolling updates can be expensive. To disable the behavior, you can change the value in the clearsContextBeforeDrawing property to NO.

Minimize graphics state changes while drawing

Changing the graphics state requires work by the underlying graphics subsystems. If you need to draw content that uses similar state information, try to draw that content together to reduce the number of state changes needed.

Use Instruments to debug your performance

The Core Animation instrument can help you spot drawing performance problems in your app. In particular:

  • Flash Updated Regions makes it easy to see what parts of your view are actually being updated.

  • Color Misaligned Images helps you see images that are aligned poorly, which results in both fuzzy images and poor performance.

For more information, see Measuring Graphics Performance in Your iOS Device in Instruments User Guide.