1

I am creating a layer with a shadow effect. Instead of adding it as a sublayer to my view's layer, I would like to draw it. But I am having problems being able to draw it with the shadow effect. The problem is that the context size it based on the layer size. But how do I set the rect for the context?!

CALayer *layer = [CALayer layer];
layer.frame = ...;
layer.backgroundColor = [UIColor redColor].CGColor;
layer.cornerRadius = 2.0;
layer.masksToBounds = NO;
layer.shadowColor = [[UIColor whiteColor] CGColor];
layer.shadowOffset = CGSizeMake(0, 1);
layer.shadowRadius = 0.5;
layer.shadowOpacity = 0.2;
[self.layer addSublayer:layer];
UIGraphicsBeginImageContext(layer.bounds.size);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[viewImage drawAtPoint:CGPointMake(0, 0)];
[layer removeFromSuperlayer];

With the code above, what I am getting is the box, but without a few extra pixels of padding with the shadow (on all four sides). If I increase the size of the ImageContext, all I get is more height and width, but still starting from x=0 and y=0, where I would want it to start from x=-5, y=-5 or something like that.

enter image description here

Thanks a bunch!

runmad
  • 14,846
  • 9
  • 99
  • 140
  • You probably have your reasons for it, but why don't you just use the layer directly, without converting it to a image first? That would solve your problem. – fishinear Jan 09 '12 at 23:17
  • Yeah, but I'd prefer to just draw a flat image directly in my view. – runmad Jan 10 '12 at 03:11
  • @runmad I'm interested in your reasons for drawing an image rather than using a layer? – ocodo Oct 31 '12 at 03:54

2 Answers2

5

Make your context 10 pixels larger in both the x and the y.

Then before you draw your layer do a CGContextTranslateCTM(context,5,5);

Hope this helps!

Nico
  • 3,826
  • 1
  • 21
  • 31
  • This doesn't quite seem to do the trick. It just moves the layer in the direction of the x and y, but doesn't expand the context area. – runmad Jan 10 '12 at 03:08
  • OK, figured it out. When using this, you also need to increase the size for the image context. – runmad Jan 10 '12 at 03:49
  • 1
    As stated, make your context 10 pixels larger in both the x and the y. – Nico Jan 10 '12 at 14:33
0

I'm not sure if the following is your problem, but it just caught my eye.

[self.layer addSublayer:layer];
UIGraphicsBeginImageContext(layer.bounds.size);
[layer renderInContext:UIGraphicsGetCurrentContext()];

It looks like you have a property on this file called layer, which you are adding the shadow layer you've created to. You then get the context based on the current CALayer, as opposed to self.layer.

I typically use CALayer to modify UIImageView, and do not have a need to add sub layers, so perhaps that code is fine. Just wanted to point it out, please do let me know if which way is accurate, as I always like to learn.

~Good Luck

Ryan Crews
  • 3,015
  • 1
  • 32
  • 28
  • Thanks for noticing that. While I do get rid of the layer from the superLayer, that was needed since I just need to grab the context from that layer before discarding it. – runmad Jan 10 '12 at 03:13