Skip to content

Instantly share code, notes, and snippets.

@SongJiaqiang
Created July 8, 2018 03:36
Show Gist options
  • Save SongJiaqiang/a7945b1c16d7987cfa089fae02f9852a to your computer and use it in GitHub Desktop.
Save SongJiaqiang/a7945b1c16d7987cfa089fae02f9852a to your computer and use it in GitHub Desktop.
绘制阴影
// 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