Skip to content

Instantly share code, notes, and snippets.

@mustafaibrahim989
Created March 11, 2015 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustafaibrahim989/3f63b2e105567a887c97 to your computer and use it in GitHub Desktop.
Save mustafaibrahim989/3f63b2e105567a887c97 to your computer and use it in GitHub Desktop.
func roundImage(#image: UIImage, withSize size: CGSize, withStorkeColor strokeColor: UIColor, withCornerRaduis raduis: CGFloat, withBorderWidth borderWidth: CGFloat) -> UIImage {
UIGraphicsBeginImageContextWithOptions(image.size, false, 0)
let borderWidth: CGFloat = borderWidth
let cornerRadius:CGFloat = raduis
let multiplier:CGFloat = image.size.height/size.height > image.size.width/size.width ?
image.size.height/size.height :
image.size.width/size.width
let borderWidthMultiplied:CGFloat = borderWidth * multiplier
let cornerRadiusMultiplied:CGFloat = cornerRadius * multiplier
UIGraphicsBeginImageContextWithOptions(image.size, false, 0)
let path = UIBezierPath(roundedRect: CGRectInset(CGRectMake(0, 0, image.size.width, image.size.height),
borderWidthMultiplied / 2, borderWidthMultiplied / 2), cornerRadius: cornerRadiusMultiplied)
let context = UIGraphicsGetCurrentContext()
CGContextSaveGState(context)
// Clip the drawing area to the path
path.addClip()
// Draw the image into the context
image.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height))
CGContextRestoreGState(context)
// Configure the stroke
strokeColor.setStroke()
path.lineWidth = borderWidthMultiplied
// Stroke the border
path.stroke()
return UIGraphicsGetImageFromCurrentImageContext();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment