Search code examples
iphoneobjective-cquartz-graphics

Drawing a rounded drop shadow with Quartz


I am trying to draw a drop shadow underneath an image to which I apply rounded corners, but I have 2 problems:

  1. The drop shadow only appears underneath the non-rounded area of the rounded image and not underneath the bottom rounded corners as one would get if applying a drop shadow in photoshop.

  2. Using the same settings as in photoshop (a 2 y-axis offset, 1 blur and 85% black) results in a much darker shadow which does not appear as blurred as it should.

Any help would be appreciated please.

float myColorValues[] = {0, 0, 0, 0.85};
    CGColorRef myColor = CGColorCreate(colorSpace, myColorValues);
    CGContextSetShadowWithColor(context, CGSizeMake(0, -2), 2, myColor);

    // Draw a round corner path
    CGContextBeginPath(context);
    CGRect rect = CGRectMake(0, 0, 68, 68);
    addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
    CGContextClosePath(context);
    CGContextClip(context);

    CGContextDrawImage(context, CGRectMake(1, 2, 70, 70), imageScaledAndCropped);

Solution

  • The solution is to draw a rounded bezier path underneath the image and to apply the shadow to that.