Skip to content

Instantly share code, notes, and snippets.

@gbarcena
Last active August 29, 2015 14:05
Show Gist options
  • Save gbarcena/e5e6ad3668bccc240a68 to your computer and use it in GitHub Desktop.
Save gbarcena/e5e6ad3668bccc240a68 to your computer and use it in GitHub Desktop.
Rounding Image Corners
-(void)setupButton:(UIButton *)button bounds:(CGRect)bounds cornerRadius:(CGFloat)cornerRadius bgColor:(UIColor *)bgColor
{
UIImage *image = [[self class] imageWithFrame:bounds
cornerRadius:cornerRadius
roundingCorners:UIRectCornerBottomLeft
bgColor:bgColor];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
}
+(UIImage *)imageWithFrame:(CGRect)frame
cornerRadius:(CGFloat)cornerRadius
roundingCorners:(UIRectCorner)roundingCorners
bgColor:(UIColor *)bgColor
{
//// General Declarations
UIGraphicsBeginImageContext(frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
UIBezierPath* roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(CGRectGetMinX(frame),
CGRectGetMinY(frame),
CGRectGetWidth(frame),
CGRectGetHeight(frame))
byRoundingCorners:roundingCorners
cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
CGContextSaveGState(context);
[bgColor setFill];
[roundedRectanglePath fill];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(context);
UIGraphicsEndImageContext();
return image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment