I have an UIView which I want to give a gradient background and I’m wondering how to implement that as efficient as possible.
I am considering three options:
-
Create an UIImageView as subview and give it an image of the gradient to display.
-
Draw the gradient image in drawRect: of the original UIView.
-
Draw the gradient in drawRect:, but this time use CoreGraphics to ‘create it from scratch’.
Which of these three would be the fastest/least memory intensive?
(The fastest to run, not to write.)
Thanks!
Things to keep in mind:
You have little-to-no control over when how often drawRect executes, so you don’t want to be doing things like memory allocation within that method.
drawRect could be called more than once in the same run loop, so it’s not a good idea to create autoreleased objects during that method either, because they may stack up and consume more memory than necessary.