Skip to content

Instantly share code, notes, and snippets.

@ksm
Created February 21, 2012 11:20
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ksm/1875946 to your computer and use it in GitHub Desktop.
Save ksm/1875946 to your computer and use it in GitHub Desktop.
CALayer shadowPath done the right way
/*
Source: Apple Developer - Understanding iOS View Compositing
*/
// setup the layer
CALayer *layer = view.layer;
layer.bounds = sublayer_bounds;
layer.backgroundColor = random_color();
// set the shadow properties on the layer
layer.shadowOpacity = 0.5;
layer.shadowRadius = 10;
layer.shadowOffset = CGSizeMake(0, 10);
// get a UIBezierPath (for its CGPath gut)
CGPathRef shadowPath = [UIBezierPath bezierPathWithRect:sublayer_bounds].CGPath;
sublayer.shadowPath = shadowPath;
// set the CGPath as the shadow layer
UIImage *image = [self loadImage];
UIColor *color = [UIColor colorWithWhite:0 alpha:0.5];
// Draw it into context (you always want to draw it into context, all in one rendering pass)
CGContextSetShadowWithColor(ctx, CGSizeMake(0,10) /* offset */, 10 /* blur radius */, color.CGColor);
CGContextDrawImage(ctx, r, image.CGImage);
@kyleclegg
Copy link

Can you clarify what ctx is?

@MaxGabriel
Copy link

the CGContext you are drawing into. CGContextRef context = UIGraphicsGetCurrentContext();

@spirejankulovski
Copy link

What's the "r" in the CGContextDrawImage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment