Last active
December 12, 2017 07:08
-
-
Save zooyf/d78c493b5258e45462d944b0364d8ce8 to your computer and use it in GitHub Desktop.
UIImageView clip showing image
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
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