2

What is the difference between CGContextDrawLinearGradient, called in a UIView's drawRect method, and a CAGradientLayer? How do they compare performance wise? What is the best practice for creating gradient views? I'd really like a nice explanation of how they relate to each other and why one is better performance than the other.

Thanks.

Stephanie
  • 145
  • 1
  • 6

1 Answers1

1

If you just want a box with a gradient in it then performance isn't really an issue. You should go with whatever is simplest to implement for your particular requirements.

Adding a CAGradientLayer means you don't have to create a view subclass, you can just add the layer to an existing view. The setup is also slightly easier, since you don't need to worry about frame sizes or any c-style core graphics functions. You can also add rounded corners, shadows etc without too much effort.

However, if you want more than one gradient view, a subclass might be a good idea, so you can just instatiate new ones.

So, unfortunately, there isn't a clear cut answer to your question. Neither is definitively better. If you are concerned with performance, implement both and test using instruments.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • Thanks. I was wondering if there was a clearcut better one to use. I will do as you suggest as just use whichever is easier. – Stephanie May 24 '12 at 15:22