Skip to content

Commit

Permalink
Bordered Jony Ive image-rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoarment committed Mar 5, 2019
1 parent 897d479 commit 2dbb4c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions FCUtilities/UIImage+FCUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- (UIImage * _Nonnull)fc_tintedImageUsingColor:(UIColor * _Nonnull)tintColor;
- (UIImage * _Nonnull)fc_imageWithRoundedCornerRadius:(CGFloat)cornerRadius;
- (UIImage * _Nonnull)fc_imageWithJonyIveRoundedCornerRadius:(CGFloat)cornerRadius;
- (UIImage * _Nonnull)fc_imageWithJonyIveRoundedCornerRadius:(CGFloat)cornerRadius borderColor:(UIColor * _Nonnull)borderColor borderWidth:(CGFloat)borderWidth;
- (UIImage * _Nonnull)fc_imagePaddedWithColor:(UIColor * _Nonnull)color insets:(UIEdgeInsets)insets;


Expand Down
25 changes: 25 additions & 0 deletions FCUtilities/UIImage+FCUtilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,31 @@ - (UIImage *)fc_imageWithJonyIveRoundedCornerRadius:(CGFloat)cornerRadius
return result;
}

- (UIImage *)fc_imageWithJonyIveRoundedCornerRadius:(CGFloat)borderCornerRadius borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth
{
CGFloat halfBorderWidth = borderWidth / 2.0f;
CGRect outputRect = CGRectMake(0, 0, self.size.width, self.size.height);
CGRect imageRect = CGRectInset(outputRect, borderWidth, borderWidth);
CGRect borderRect = CGRectInset(outputRect, halfBorderWidth, halfBorderWidth);
UIGraphicsBeginImageContextWithOptions(outputRect.size, NO, self.scale);
CGFloat imageCornerRadius = MAX(0, borderCornerRadius - borderWidth);

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
[[UIBezierPath bezierPathWithRoundedRect:imageRect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(imageCornerRadius, imageCornerRadius)] addClip];
[self drawInRect:imageRect];
CGContextRestoreGState(ctx);

UIBezierPath *borderPath = [UIBezierPath bezierPathWithRoundedRect:borderRect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(borderCornerRadius, borderCornerRadius)];
borderPath.lineWidth = borderWidth;
[borderColor setStroke];
[borderPath stroke];

UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}

- (UIImage * _Nonnull)fc_imagePaddedWithColor:(UIColor * _Nonnull)color insets:(UIEdgeInsets)insets
{
// Only ever adds size to the image, doesn't remove it
Expand Down

0 comments on commit 2dbb4c5

Please sign in to comment.