Skip to content

Commit 658b914

Browse files
committed
Fix default error or default success image was nil when first to invoke.
1 parent 60d0ed8 commit 658b914

2 files changed

Lines changed: 132 additions & 74 deletions

File tree

Demo/WSProgressHUD/WSProgressHUD.m

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ + (void)showWithStatus: (NSString *)string maskType: (WSProgressHUDMaskType)mask
140140

141141
+ (void)showSuccessWithStatus: (NSString *)string
142142
{
143-
[self showImage:WSProgressHUDSuccessImage status:string];
143+
[self showImage:WSProgressHUDSuccessDefaultImage() status:string];
144144
}
145145

146146
+ (void)showErrorWithStatus: (NSString *)string
147147
{
148-
[self showImage:WSProgressHUDErrorImage status:string];
148+
[self showImage:WSProgressHUDErrorDefaultImage() status:string];
149149
}
150150

151151

@@ -287,11 +287,11 @@ - (void)showImage:(UIImage *)image status:(NSString *)title maskType: (WSProgres
287287

288288
- (void)showSuccessWithString: (NSString *)string
289289
{
290-
[self showImage:WSProgressHUDSuccessImage status:string];
290+
[self showImage:WSProgressHUDSuccessDefaultImage() status:string];
291291
}
292292
- (void)showErrorWithString: (NSString *)string
293293
{
294-
[self showImage:WSProgressHUDErrorImage status:string];
294+
[self showImage:WSProgressHUDErrorDefaultImage() status:string];
295295
}
296296

297297
- (void)showWithMaskType: (WSProgressHUDMaskType)maskType maskWithout: (WSProgressHUDMaskWithoutType)withoutType
@@ -855,8 +855,8 @@ - (void)exchangeIndicatorSizeToBig:(BOOL)big
855855
[self.ringLayer removeFromSuperlayer];
856856
[self.backgroundRingLayer removeFromSuperlayer];
857857

858-
self.ringLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDForeGroundColor];
859-
self.backgroundRingLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDBackGroundColor];
858+
self.ringLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDForeGroundDefaultColor()];
859+
self.backgroundRingLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDBackGroundDefaultColor()];
860860
self.ringLayer.strokeEnd = 0;
861861
self.backgroundRingLayer.strokeEnd = 1;
862862
[self.hudView.layer addSublayer:self.backgroundRingLayer];
@@ -1031,23 +1031,6 @@ - (void)setMaskEdgeWithType: (WSProgressHUDMaskType)maskType
10311031
}
10321032
}
10331033

1034-
1035-
1036-
- (UIImage *)image:(UIImage *)image withTintColor:(UIColor *)color{
1037-
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
1038-
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
1039-
CGContextRef c = UIGraphicsGetCurrentContext();
1040-
[image drawInRect:rect];
1041-
CGContextSetFillColorWithColor(c, [color CGColor]);
1042-
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
1043-
CGContextFillRect(c, rect);
1044-
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
1045-
UIGraphicsEndImageContext();
1046-
1047-
return tintedImage;
1048-
}
1049-
1050-
10511034
- (void)invalidateTimer
10521035
{
10531036
if (self.timer) {
@@ -1243,19 +1226,6 @@ - (instancetype)initWithFrame:(CGRect)frame
12431226
self = [super initWithFrame:frame];
12441227
if (self) {
12451228

1246-
WSProgressHUDForeGroundColor = [UIColor whiteColor];
1247-
WSProgressHUDBackGroundColor = [UIColor colorWithWhite:0.3 alpha:1];
1248-
1249-
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
1250-
NSURL *bundleUrl = [bundle URLForResource:@"WSProgressBundle" withExtension:@"bundle"];
1251-
NSBundle *imageBundle = [NSBundle bundleWithURL:bundleUrl];
1252-
1253-
UIImage *successImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"success@2x" ofType:@"png"]];
1254-
UIImage *failurImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"error@2x" ofType:@"png"]];
1255-
1256-
WSProgressHUDSuccessImage = [self image:successImage withTintColor:WSProgressHUDForeGroundColor];
1257-
WSProgressHUDErrorImage = [self image:failurImage withTintColor:WSProgressHUDForeGroundColor];
1258-
12591229
[self addSubview:self.hudView];
12601230

12611231
[self.hudView addSubview:self.indicatorView];
@@ -1278,6 +1248,65 @@ - (instancetype)initWithFrame:(CGRect)frame
12781248
return self;
12791249
}
12801250

1251+
#pragma mark - INLine Utils Method
1252+
1253+
CG_INLINE UIColor * WSProgressHUDForeGroundDefaultColor()
1254+
{
1255+
if (WSProgressHUDForeGroundColor == nil) {
1256+
WSProgressHUDForeGroundColor = [UIColor whiteColor];
1257+
}
1258+
return WSProgressHUDForeGroundColor;
1259+
}
1260+
1261+
CG_INLINE UIColor * WSProgressHUDBackGroundDefaultColor()
1262+
{
1263+
if (WSProgressHUDBackGroundColor == nil) {
1264+
WSProgressHUDBackGroundColor = [UIColor colorWithWhite:0.3 alpha:1];
1265+
}
1266+
return WSProgressHUDBackGroundColor;
1267+
}
1268+
1269+
1270+
CG_INLINE UIImage * WSProgressHUDImageWithName(NSString *imageName, NSString *imageType)
1271+
{
1272+
NSBundle *bundle = [NSBundle bundleForClass:[WSProgressHUD class]];
1273+
NSURL *bundleUrl = [bundle URLForResource:@"WSProgressBundle" withExtension:@"bundle"];
1274+
NSBundle *defaultBundle = [NSBundle bundleWithURL:bundleUrl];
1275+
return [UIImage imageWithContentsOfFile:[defaultBundle pathForResource:imageName ofType:imageType]];
1276+
}
1277+
1278+
CG_INLINE UIImage * WSProgressHUDSuccessDefaultImage()
1279+
{
1280+
if (WSProgressHUDSuccessImage == nil) {
1281+
UIImage *successImage = WSProgressHUDImageWithName(@"success@2x", @"png");
1282+
WSProgressHUDSuccessImage = WSImageByAddTintColr(successImage, WSProgressHUDForeGroundDefaultColor());
1283+
}
1284+
return WSProgressHUDSuccessImage;
1285+
}
1286+
1287+
CG_INLINE UIImage * WSProgressHUDErrorDefaultImage()
1288+
{
1289+
if (WSProgressHUDErrorImage == nil) {
1290+
UIImage *failurImage = WSProgressHUDImageWithName(@"error@2x", @"png");
1291+
WSProgressHUDErrorImage = WSImageByAddTintColr(failurImage, WSProgressHUDForeGroundDefaultColor());
1292+
}
1293+
return WSProgressHUDErrorImage;
1294+
}
1295+
1296+
1297+
CG_INLINE UIImage * WSImageByAddTintColr(UIImage *image, UIColor *color)
1298+
{
1299+
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
1300+
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
1301+
CGContextRef c = UIGraphicsGetCurrentContext();
1302+
[image drawInRect:rect];
1303+
CGContextSetFillColorWithColor(c, [color CGColor]);
1304+
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
1305+
CGContextFillRect(c, rect);
1306+
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
1307+
UIGraphicsEndImageContext();
1308+
return tintedImage;
1309+
}
12811310

12821311
- (UIView *)hudView
12831312
{
@@ -1373,7 +1402,7 @@ - (WSIndefiniteAnimationView *)indefiniteAnimationView
13731402
{
13741403
if (!_indefiniteAnimationView) {
13751404
_indefiniteAnimationView = [[WSIndefiniteAnimationView alloc] initWithFrame:CGRectZero];
1376-
_indefiniteAnimationView.strokeColor = WSProgressHUDForeGroundColor;
1405+
_indefiniteAnimationView.strokeColor = WSProgressHUDForeGroundDefaultColor();
13771406
_indefiniteAnimationView.strokeThickness = WSProgressHUDRingThickness;
13781407
_indefiniteAnimationView.radius = 10;
13791408
[_indefiniteAnimationView sizeToFit];

WSProgressHUD/WSProgressHUD.m

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,12 @@ + (void)showWithStatus: (NSString *)string maskType: (WSProgressHUDMaskType)mask
140140

141141
+ (void)showSuccessWithStatus: (NSString *)string
142142
{
143-
[self showImage:WSProgressHUDSuccessImage status:string];
143+
[self showImage:WSProgressHUDSuccessDefaultImage() status:string];
144144
}
145145

146146
+ (void)showErrorWithStatus: (NSString *)string
147147
{
148-
[self showImage:WSProgressHUDErrorImage status:string];
148+
[self showImage:WSProgressHUDErrorDefaultImage() status:string];
149149
}
150150

151151

@@ -287,11 +287,11 @@ - (void)showImage:(UIImage *)image status:(NSString *)title maskType: (WSProgres
287287

288288
- (void)showSuccessWithString: (NSString *)string
289289
{
290-
[self showImage:WSProgressHUDSuccessImage status:string];
290+
[self showImage:WSProgressHUDSuccessDefaultImage() status:string];
291291
}
292292
- (void)showErrorWithString: (NSString *)string
293293
{
294-
[self showImage:WSProgressHUDErrorImage status:string];
294+
[self showImage:WSProgressHUDErrorDefaultImage() status:string];
295295
}
296296

297297
- (void)showWithMaskType: (WSProgressHUDMaskType)maskType maskWithout: (WSProgressHUDMaskWithoutType)withoutType
@@ -855,8 +855,8 @@ - (void)exchangeIndicatorSizeToBig:(BOOL)big
855855
[self.ringLayer removeFromSuperlayer];
856856
[self.backgroundRingLayer removeFromSuperlayer];
857857

858-
self.ringLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDForeGroundColor];
859-
self.backgroundRingLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDBackGroundColor];
858+
self.ringLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDForeGroundDefaultColor()];
859+
self.backgroundRingLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDBackGroundDefaultColor()];
860860
self.ringLayer.strokeEnd = 0;
861861
self.backgroundRingLayer.strokeEnd = 1;
862862
[self.hudView.layer addSublayer:self.backgroundRingLayer];
@@ -1031,23 +1031,6 @@ - (void)setMaskEdgeWithType: (WSProgressHUDMaskType)maskType
10311031
}
10321032
}
10331033

1034-
1035-
1036-
- (UIImage *)image:(UIImage *)image withTintColor:(UIColor *)color{
1037-
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
1038-
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
1039-
CGContextRef c = UIGraphicsGetCurrentContext();
1040-
[image drawInRect:rect];
1041-
CGContextSetFillColorWithColor(c, [color CGColor]);
1042-
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
1043-
CGContextFillRect(c, rect);
1044-
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
1045-
UIGraphicsEndImageContext();
1046-
1047-
return tintedImage;
1048-
}
1049-
1050-
10511034
- (void)invalidateTimer
10521035
{
10531036
if (self.timer) {
@@ -1243,19 +1226,6 @@ - (instancetype)initWithFrame:(CGRect)frame
12431226
self = [super initWithFrame:frame];
12441227
if (self) {
12451228

1246-
WSProgressHUDForeGroundColor = [UIColor whiteColor];
1247-
WSProgressHUDBackGroundColor = [UIColor colorWithWhite:0.3 alpha:1];
1248-
1249-
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
1250-
NSURL *bundleUrl = [bundle URLForResource:@"WSProgressBundle" withExtension:@"bundle"];
1251-
NSBundle *imageBundle = [NSBundle bundleWithURL:bundleUrl];
1252-
1253-
UIImage *successImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"success@2x" ofType:@"png"]];
1254-
UIImage *failurImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"error@2x" ofType:@"png"]];
1255-
1256-
WSProgressHUDSuccessImage = [self image:successImage withTintColor:WSProgressHUDForeGroundColor];
1257-
WSProgressHUDErrorImage = [self image:failurImage withTintColor:WSProgressHUDForeGroundColor];
1258-
12591229
[self addSubview:self.hudView];
12601230

12611231
[self.hudView addSubview:self.indicatorView];
@@ -1278,6 +1248,65 @@ - (instancetype)initWithFrame:(CGRect)frame
12781248
return self;
12791249
}
12801250

1251+
#pragma mark - INLine Utils Method
1252+
1253+
CG_INLINE UIColor * WSProgressHUDForeGroundDefaultColor()
1254+
{
1255+
if (WSProgressHUDForeGroundColor == nil) {
1256+
WSProgressHUDForeGroundColor = [UIColor whiteColor];
1257+
}
1258+
return WSProgressHUDForeGroundColor;
1259+
}
1260+
1261+
CG_INLINE UIColor * WSProgressHUDBackGroundDefaultColor()
1262+
{
1263+
if (WSProgressHUDBackGroundColor == nil) {
1264+
WSProgressHUDBackGroundColor = [UIColor colorWithWhite:0.3 alpha:1];
1265+
}
1266+
return WSProgressHUDBackGroundColor;
1267+
}
1268+
1269+
1270+
CG_INLINE UIImage * WSProgressHUDImageWithName(NSString *imageName, NSString *imageType)
1271+
{
1272+
NSBundle *bundle = [NSBundle bundleForClass:[WSProgressHUD class]];
1273+
NSURL *bundleUrl = [bundle URLForResource:@"WSProgressBundle" withExtension:@"bundle"];
1274+
NSBundle *defaultBundle = [NSBundle bundleWithURL:bundleUrl];
1275+
return [UIImage imageWithContentsOfFile:[defaultBundle pathForResource:imageName ofType:imageType]];
1276+
}
1277+
1278+
CG_INLINE UIImage * WSProgressHUDSuccessDefaultImage()
1279+
{
1280+
if (WSProgressHUDSuccessImage == nil) {
1281+
UIImage *successImage = WSProgressHUDImageWithName(@"success@2x", @"png");
1282+
WSProgressHUDSuccessImage = WSImageByAddTintColr(successImage, WSProgressHUDForeGroundDefaultColor());
1283+
}
1284+
return WSProgressHUDSuccessImage;
1285+
}
1286+
1287+
CG_INLINE UIImage * WSProgressHUDErrorDefaultImage()
1288+
{
1289+
if (WSProgressHUDErrorImage == nil) {
1290+
UIImage *failurImage = WSProgressHUDImageWithName(@"error@2x", @"png");
1291+
WSProgressHUDErrorImage = WSImageByAddTintColr(failurImage, WSProgressHUDForeGroundDefaultColor());
1292+
}
1293+
return WSProgressHUDErrorImage;
1294+
}
1295+
1296+
1297+
CG_INLINE UIImage * WSImageByAddTintColr(UIImage *image, UIColor *color)
1298+
{
1299+
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
1300+
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
1301+
CGContextRef c = UIGraphicsGetCurrentContext();
1302+
[image drawInRect:rect];
1303+
CGContextSetFillColorWithColor(c, [color CGColor]);
1304+
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
1305+
CGContextFillRect(c, rect);
1306+
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
1307+
UIGraphicsEndImageContext();
1308+
return tintedImage;
1309+
}
12811310

12821311
- (UIView *)hudView
12831312
{
@@ -1373,7 +1402,7 @@ - (WSIndefiniteAnimationView *)indefiniteAnimationView
13731402
{
13741403
if (!_indefiniteAnimationView) {
13751404
_indefiniteAnimationView = [[WSIndefiniteAnimationView alloc] initWithFrame:CGRectZero];
1376-
_indefiniteAnimationView.strokeColor = WSProgressHUDForeGroundColor;
1405+
_indefiniteAnimationView.strokeColor = WSProgressHUDForeGroundDefaultColor();
13771406
_indefiniteAnimationView.strokeThickness = WSProgressHUDRingThickness;
13781407
_indefiniteAnimationView.radius = 10;
13791408
[_indefiniteAnimationView sizeToFit];

0 commit comments

Comments
 (0)