Skip to content

Instantly share code, notes, and snippets.

@AlexFlyce
Created May 22, 2018 14:31
Show Gist options
  • Save AlexFlyce/5b35b1ae22232117b326973dfdeeef7b to your computer and use it in GitHub Desktop.
Save AlexFlyce/5b35b1ae22232117b326973dfdeeef7b to your computer and use it in GitHub Desktop.
ART001-circularImage.swift
extension UIImage {
class func circularImage(from image: UIImage, size: CGSize) -> UIImage? {
let scale = UIScreen.main.scale
let circleRect = CGRect(x: 0, y: 0, width: size.width * scale, height: size.height * scale)
UIGraphicsBeginImageContextWithOptions(circleRect.size, false, scale)
let circlePath = UIBezierPath(roundedRect: circleRect, cornerRadius: circleRect.size.width/2.0)
circlePath.addClip()
image.draw(in: circleRect)
if let roundImage = UIGraphicsGetImageFromCurrentImageContext() {
return roundImage
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment