Search code examples
uiviewdrawrect

How to speed drawing a grid of UIViews?


Currently I instantiate a 2-d matrix of UIViews. Each UIView's drawRect is overridden to draw 1 of 2-3 shapes.

As the grid scales larger, I am noticing excessive time spent in the drawRect of each subview. Since I only have 2-3 shapes, I would like to speed up the rendering of the matrix by drawing the 2-3 unique UIViews each only one time, and then somehow instantiate a copy of the appropriate pre-drawn UIView in the matrix.

I have considered capturing the UIView as a UIImage, making a copy of the UIImage and instantiating this copy. I am wondering though, if the overhead of this process makes it not appreciably faster than drawRect.

Can someone point me to a best practice for speed optimization by reusing UIViews in a 2-D matrix?


Solution

  • The quick fix to this issue is to enable the shouldRasterize property of a given UIView that does not require further updates.