3

I'm trying to draw some shadows in a rect. The shadow image itself is about 1px * 26px.

Here's two methods I've thought of for drawing the image in the view:

//These methods are called in drawRect:

/* Method 1 */
[self.upperShadow drawInRect:rectHigh]; //upperShadow is UIImage
[self.lowerShadow drawInRect:rectLow];


/* Method 2 */
CALayer *shadowTop = [CALayer layer];
shadowTop.frame = rectHigh;
shadowTop.contents = (__bridge id)topShadow; //topShadow is CGImage
[self.layer addSublayer:shadowTop];
CALayer *shadowLow = [CALayer layer];
shadowLow.frame = rectLow;
shadowLow.contents = (__bridge id)lowShadow;
[self.layer addSublayer:shadowLow];

/* Method 3 */    
UIImageView *tShadow = [[UIImageView alloc] initWithFrame:rectHigh];
UIImageView *bShadow = [[UIImageView alloc] initWithFrame:rectLow];
tShadow.image = self.upperShadow;
bShadow.image = self.lowerShadow;
tShadow.contentMode = UIViewContentModeScaleToFill;
bShadow.contentMode = UIViewContentModeScaleToFill;
[self addSubview:tShadow];
[self addSubview:bShadow];

I'm curious which of these is better, when it comes to performance in drawing and animation. From my benchmarking it seems that the layers are faster to draw. Here are some benchmarking stats:

drawInRect: took 0.00054 secs 
CALayers took 0.00006 secs 
UIImageView took 0.00017 secs

The view which contains these shadows is going to have a view above it which will be animated (the view itself is not). Anything that would degrade the animation performance should be avoided. Any thoughts between the three methods?

sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161

1 Answers1

2

If the shadows are static, then the best way is to use two UIImageViews. It's even smarter than CALayer about how to deal with static images (though I don't know if that's going to make a difference here), and will otherwise have the same benefits as CALayer, such as having all compositing being done on the GPU instead of on the CPU (as your Method 2 will require).

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • Are you sure? It seems that creating `UIImageView`s would be an unnecessary overhead, both in memory usage as well as in CPU usage. Am I wrong? And yes, the shadows are static. They didn't used to be, as I used to draw them dynamically by creating a bezier path and creating a shadow from the path, but it was just too slow so I'm switching to static images. – sudo rm -rf Jan 07 '12 at 05:23
  • @sudorm-rf: You are wrong. Views are rather lightweight wrappers over layers, and layers themselves are actually pretty light. Letting the GPU do the compositing (which is what layers gets you) is *significantly* better than redrawing it in code every time anything changes. – Lily Ballard Jan 07 '12 at 05:24
  • Well, it just seems to me that if `UIImageView` is just a wrapper over a layer, then why not just create a layer and be done with it? This view isn't going to be redrawn during it's lifetime. – sudo rm -rf Jan 07 '12 at 05:26
  • @sudorm-rf: Note that if you just want your layer to have a shadow, then you can let CALayer draw the shadow by using the relevant properties. If you do that, then you should create a bezier path that represents the area you want shadowed and set that as the `shadowPath` property, or performance won't be good. That said, static images are still better if they're suitable for your needs. – Lily Ballard Jan 07 '12 at 05:26
  • That's what I did before. However, I didn't set the `shadowPath` property, so perhaps that was why my performance issue was encountered in the first place. – sudo rm -rf Jan 07 '12 at 05:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6499/discussion-between-kevin-ballard-and-sudo-rm-rf) – Lily Ballard Jan 07 '12 at 05:28