Skip to content

Instantly share code, notes, and snippets.

@zooyf
Last active December 12, 2017 07:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zooyf/d78c493b5258e45462d944b0364d8ce8 to your computer and use it in GitHub Desktop.
UIImageView clip showing image
extension UIImageView {
func clip(roundedRect: CGRect?, cornerRadius: CGFloat = 0, force: Bool = false) {
if force {
let bezierPath = UIBezierPath(roundedRect: roundedRect ?? bounds, cornerRadius: cornerRadius)
let maskLayer = CAShapeLayer()
maskLayer.path = bezierPath.cgPath
layer.mask = maskLayer
} else if layer.mask == nil {
let bezierPath = UIBezierPath(roundedRect: roundedRect ?? bounds, cornerRadius: cornerRadius)
let maskLayer = CAShapeLayer()
maskLayer.path = bezierPath.cgPath
layer.mask = maskLayer
}
}
func clip(cornerRadius: CGFloat = 0, force: Bool = false) {
clip(roundedRect: nil, cornerRadius: cornerRadius, force: force)
}
func clip(cornerRadius: CGFloat!) {
clip(cornerRadius: cornerRadius, force: false)
}
func clip(force: Bool!) {
clip(cornerRadius: 0, force: force)
}
func clipsToCircle() {
let radius = bounds.width < bounds.height ? bounds.width : bounds.height
clip(cornerRadius: radius/2.0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment