Created
July 8, 2018 03:36
-
-
Save SongJiaqiang/a7945b1c16d7987cfa089fae02f9852a to your computer and use it in GitHub Desktop.
绘制阴影
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://blog.helftone.com/demystifying-inner-shadows-in-quartz/ | |
// Drawing the shadow in the same image | |
func roundedImage(for image: UIImage, bounds: CGRect, withShadow shadow: Bool) -> UIImage { | |
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0) | |
defer { | |
UIGraphicsEndImageContext() | |
} | |
let context = UIGraphicsGetCurrentContext() | |
let circle = CGPathCreateWithEllipseInRect(bounds, nil) | |
if shadow { | |
// draw an elliptical shadow | |
CGContextSaveGState(context) | |
CGContextSetShadowWithColor(context, .zero, 5.0, UIColor.blackColor().CGColor) | |
CGContextAddPath(context, circle) | |
CGContextFillPath(context) | |
CGContextRestoreGState(context) | |
} | |
// clip to an elliptical shape, and draw the image | |
CGContextAddPath(context, circle) | |
CGContextClip(context) | |
image.drawInRect(bounds) | |
return UIGraphicsGetImageFromCurrentImageContext() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment