4

I am experimenting with a few things for a new UI in one of my apps. I'm wondering, what is the most efficient way to add UI elements like a gradient background (keeping in mind both the size of the app as well as speed/memory consumption).

Would I be better off just creating the elements in something like Inkscape or Illustrator, and then loading them in via UIImage>UIIMageView? Or would it be best to use CGGradient or CAGradientLayer?

Also, I wonder about these kinds of things quite often and I'm wondering if someone could explain how I could test these kinds of things for myself (speed, memory consumption, overall performance).

Community
  • 1
  • 1
daveMac
  • 3,041
  • 3
  • 34
  • 59

1 Answers1

3

CAGradientLayers are pretty good.I'd nearly always favour drawing gradients using CGGradient or using a CAGradientLayer over using images, particularly since you then don't need to include retina/non-retina, iPhone/iPad combinations, and changing the colours becomes a matter of tweaking one line of code rather than regenerating a whole series of images in your imaging software.

If you're doing other drawing in drawRect as well, draw with a CGGradient. If you just want a background, use CAGradientLayer.

You can analyse the performance using Instruments (product --> profile in XCode)

  • the core animation tool will give you frames per second if there is scrolling / animation
  • the time profiler tool will show you which functions are taking up your time
  • the various memory tools will show you memory consumption
jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Thanks jrturton. Any idea (without me checking myself) which is better for performance? – daveMac Jun 12 '12 at 16:33
  • It really depends what else you're doing with the view. I'd start with the simplest implementation (cagradientlayer, IMO) and look elsewhere if the performance isn't satisfactory. If you're not scrolling or animating then the performance is irrelevant. – jrturton Jun 12 '12 at 17:14