Skip to content

Commit 1f9fd5a

Browse files
committed
RFDrawImage, add imageSize guard, improve draw result with stroke.
1 parent 8f4a340 commit 1f9fd5a

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

Diff for: RFDrawImage/RFDrawImage.h

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
/*!
2-
RFDrawImage
3-
RFUI
4-
5-
Copyright (c) 2014 BB9z
6-
https://github.com/RFUI/Alpha
7-
8-
The MIT License (MIT)
9-
http://www.opensource.org/licenses/mit-license.php
10-
11-
BETA
2+
RFDrawImage
3+
RFUI
4+
5+
Copyright (c) 2014, 2018 BB9z
6+
https://github.com/RFUI/Alpha
7+
8+
The MIT License (MIT)
9+
http://www.opensource.org/licenses/mit-license.php
1210
*/
1311
#import <RFKit/RFRuntime.h>
1412

1513
@interface RFDrawImage : NSObject
1614

17-
+ (UIImage *)imageWithSizeColor:(CGSize)imageSize fillColor:(UIColor *)color;
15+
+ (nonnull UIImage *)imageWithSizeColor:(CGSize)imageSize fillColor:(nonnull UIColor *)color;
1816

19-
+ (UIImage *)imageWithRoundingCorners:(UIEdgeInsets)cornerRadius
17+
+ (nonnull UIImage *)imageWithRoundingCorners:(UIEdgeInsets)cornerRadius
2018
size:(CGSize)imageSize
21-
fillColor:(UIColor *)fillColor
22-
strokeColor:(UIColor *)strokeColor
19+
fillColor:(nonnull UIColor *)fillColor
20+
strokeColor:(nullable UIColor *)strokeColor
2321
strokeWidth:(CGFloat)strokeWidth
2422
boxMargin:(UIEdgeInsets)boxMargin
2523
resizableCapInsets:(UIEdgeInsets)resizableCapInset

Diff for: RFDrawImage/RFDrawImage.m

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
@implementation RFDrawImage
55

66
+ (UIImage *)imageWithSizeColor:(CGSize)imageSize fillColor:(UIColor *)color {
7+
NSParameterAssert(imageSize.width > 0 && imageSize.height > 0);
8+
79
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
810
CGContextRef context = UIGraphicsGetCurrentContext();
911

@@ -16,7 +18,8 @@ + (UIImage *)imageWithSizeColor:(CGSize)imageSize fillColor:(UIColor *)color {
1618
}
1719

1820
+ (UIImage *)imageWithRoundingCorners:(UIEdgeInsets)cornerRadius size:(CGSize)imageSize fillColor:(UIColor *)fillColor strokeColor:(UIColor *)strokeColor strokeWidth:(CGFloat)strokeWidth boxMargin:(UIEdgeInsets)margin resizableCapInsets:(UIEdgeInsets)resizableCapInsets scaleFactor:(CGFloat)scaleFactor {
19-
21+
NSParameterAssert(imageSize.width > 0 && imageSize.height > 0);
22+
2023
UIGraphicsBeginImageContextWithOptions(imageSize, NO, scaleFactor);
2124

2225
CGFloat r_lt = cornerRadius.top;
@@ -42,12 +45,15 @@ + (UIImage *)imageWithRoundingCorners:(UIEdgeInsets)cornerRadius size:(CGSize)im
4245
[path addArcWithCenter:c_rt radius:r_rt startAngle:0 endAngle:M_PI_2 *3 clockwise:NO];
4346

4447
[path closePath];
48+
[path addClip]; // Stop stroke draw outside
4549
[fillColor setFill];
4650
[path fill];
4751

48-
[strokeColor setStroke];
49-
path.lineWidth = strokeWidth;
50-
[path stroke];
52+
if (strokeWidth > 0) {
53+
[strokeColor setStroke];
54+
path.lineWidth = strokeWidth * 2; // Double due to the chip
55+
[path stroke];
56+
}
5157

5258
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
5359
UIGraphicsEndImageContext();

0 commit comments

Comments
 (0)