Skip to content

Commit 99aa6ef

Browse files
author
Vitaliy G
committed
add new method for getting resized image with color
1 parent cd33bd4 commit 99aa6ef

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

QMChatViewController/Categories/UIImage+QM.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,14 @@
1919
*/
2020
- (UIImage *)imageMaskedWithColor:(UIColor *)maskColor;
2121

22+
/**
23+
* Creates a resizable image with specified color and corner radius
24+
*
25+
* @param color color for mask
26+
*
27+
* @return masked image
28+
*/
29+
+ (UIImage *)resizableImageWithColor:(UIColor *)color
30+
cornerRadius:(CGFloat)cornerRadius;
31+
2232
@end

QMChatViewController/Categories/UIImage+QM.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,32 @@ - (UIImage *)imageMaskedWithColor:(UIColor *)maskColor {
3535
return newImage;
3636
}
3737

38+
+ (UIImage *)resizableImageWithColor:(UIColor *)color
39+
cornerRadius:(CGFloat)cornerRadius {
40+
41+
CGFloat scale = UIScreen.mainScreen.scale;
42+
CGFloat size = 1.0 + 2 * cornerRadius;
43+
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size, size), NO, scale);
44+
CGContextRef context = UIGraphicsGetCurrentContext();
45+
CGContextSetFillColorWithColor(context, color.CGColor);
46+
CGMutablePathRef path = CGPathCreateMutable();
47+
CGPathMoveToPoint(path, NULL, cornerRadius + 1.0, 0.0);
48+
CGPathAddArcToPoint(path, NULL, size, 0.0, size, cornerRadius, cornerRadius);
49+
CGPathAddLineToPoint(path, NULL, size, cornerRadius + 1.0);
50+
CGPathAddArcToPoint(path, NULL, size, size, cornerRadius + 1.0, size, cornerRadius);
51+
CGPathAddLineToPoint(path, NULL, cornerRadius, size);
52+
CGPathAddArcToPoint(path, NULL, 0.0, size, 0.0, cornerRadius + 1.0, cornerRadius);
53+
CGPathAddLineToPoint(path, NULL, 0.0, cornerRadius);
54+
CGPathAddArcToPoint(path, NULL, 0.0, 0.0, cornerRadius, 0.0, cornerRadius);
55+
CGPathCloseSubpath(path);
56+
CGContextAddPath(context, path);
57+
CGPathRelease(path);
58+
CGContextFillPath(context);
59+
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
60+
UIGraphicsEndImageContext();
61+
62+
return [image resizableImageWithCapInsets:UIEdgeInsetsMake(cornerRadius, cornerRadius, cornerRadius, cornerRadius)
63+
resizingMode:UIImageResizingModeStretch];
64+
}
65+
3866
@end

0 commit comments

Comments
 (0)