Skip to content

Instantly share code, notes, and snippets.

@guzhenhuaGitHub
Created May 20, 2019 10:16
Show Gist options
  • Save guzhenhuaGitHub/d23b1f069de554b8d0eb8e44f0e8238d to your computer and use it in GitHub Desktop.
Save guzhenhuaGitHub/d23b1f069de554b8d0eb8e44f0e8238d to your computer and use it in GitHub Desktop.
public extension UIImage {
/// 圆角图片
func roundCornerImage(withCornerRadius cornerRadius: CGFloat) -> UIImage? {
// 开关图形上下文
UIGraphicsBeginImageContext(size)
defer { UIGraphicsEndImageContext() }
// 获取图形上下文
let context = UIGraphicsGetCurrentContext()
// 带圆角的长方形贝塞尔曲线
let roundedRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
let path = UIBezierPath(roundedRect: roundedRect, cornerRadius: cornerRadius)
// 在图形上下文中添加贝塞尔曲线
context?.addPath(path.cgPath)
// 在上下文中沿贝塞尔曲线切出圆角
context?.clip()
// 将图片画在上下文中
draw(in: roundedRect)
// 最后返回上下文中图片
return UIGraphicsGetImageFromCurrentImageContext()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment