Created
May 20, 2019 10:16
-
-
Save guzhenhuaGitHub/d23b1f069de554b8d0eb8e44f0e8238d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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