gbarcena
8/12/2014 - 9:15 PM

Rounding Image Corners

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;
}