Created
March 11, 2015 17:59
-
-
Save mustafaibrahim989/3f63b2e105567a887c97 to your computer and use it in GitHub Desktop.
This file contains 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
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