For swaths of color, which is cheaper: UIView or U

2019-07-17 17:05发布

问题:

Suppose in your UI design, you need a rectangle of solid color. Of course you could use a UIImageView object and hook it up to an image, but in some cases it might be quicker and easier to just drop in a UIView and set the background color.

My question is with regard to which is cheaper in terms of memory and performance. My gut is that an empty UIView is easier to render than an image, whose file must be loaded and bits stored in memory.

Can anyone speak to this? Many thanks!

回答1:

Setting the backgroundColor of a UIView is faster/cheaper than using a UIImageView, for the obvious reasons: you don't have to create or load an image, the window server doesn't have to keep a copy of the image around, and the window server's code for filling a view with its background color is certainly at least as fast as its code for filling a view with a scaled 1x1 image.