0

I have a bunch of UIViews to which I add shadows via their layers, in their drawRect method:

self.layer.shadowPath = path;
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowOpacity = .6;
self.layer.shadowOffset = CGSizeMake(2,3);
self.layer.shadowRadius = 2;

This works well, but my problem is I also need to create a PDF with those views. I'm doing this by creating a PDF context and passing it to the drawing method so that the drawing happens in the PDF context. That also works well, except that the shadows are not rendered in the PDF. I've experimented with a couple approaches, but haven't managed to find a proper, easy way to get those shadows to appear where they belong in the PDF.

Would anyone know how to do this?

1 Answers1

1

You will need to make the relevant CoreGraphics calls in the drawrect to draw the shadows rather than using the CALayer properties.

Check out the Apple docs on shadows.

Simon Lee
  • 22,304
  • 4
  • 41
  • 45
  • Thx Simon for the quick reply. I feared that :) - Using CGContextSetShadow, is there a way to prevent it from adding shadows to strokes and subviews? – Jerome Cordiez Jan 25 '12 at 15:24
  • Well, what you do is save / restore the context. So say you want to draw a shadow on a single rect, you use CGContextSaveState to save the current state, then add the shadow to the context, draw your rect and then use CGContextRestoreState to restore the context to how it was before you added the shadow properties, you can then continue to layer up your drawing to get the desired result. – Simon Lee Jan 25 '12 at 15:34
  • You can call the save / restore at any point, so you can do some drawing then save, add shadow, restore, continue etc etc, same applies for clipping. – Simon Lee Jan 25 '12 at 15:35
  • awesome thanks! But for stroke and fills, this means I need to separate the 2 operations? ie dump `CGContextDrawPath(context, kCGPathFillStroke );` and add the path twice and do stroke and fill separately? Trying that now, seems to be working, but I'd like to make sure I'm doing this right. – Jerome Cordiez Jan 25 '12 at 15:41