Skip to content

Commit

Permalink
Fix default error or default success image was nil when first to invoke.
Browse files Browse the repository at this point in the history
  • Loading branch information
devSC committed Jul 30, 2016
1 parent 60d0ed8 commit 658b914
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 74 deletions.
103 changes: 66 additions & 37 deletions Demo/WSProgressHUD/WSProgressHUD.m
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ + (void)showWithStatus: (NSString *)string maskType: (WSProgressHUDMaskType)mask

+ (void)showSuccessWithStatus: (NSString *)string
{
[self showImage:WSProgressHUDSuccessImage status:string];
[self showImage:WSProgressHUDSuccessDefaultImage() status:string];
}

+ (void)showErrorWithStatus: (NSString *)string
{
[self showImage:WSProgressHUDErrorImage status:string];
[self showImage:WSProgressHUDErrorDefaultImage() status:string];
}


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

- (void)showSuccessWithString: (NSString *)string
{
[self showImage:WSProgressHUDSuccessImage status:string];
[self showImage:WSProgressHUDSuccessDefaultImage() status:string];
}
- (void)showErrorWithString: (NSString *)string
{
[self showImage:WSProgressHUDErrorImage status:string];
[self showImage:WSProgressHUDErrorDefaultImage() status:string];
}

- (void)showWithMaskType: (WSProgressHUDMaskType)maskType maskWithout: (WSProgressHUDMaskWithoutType)withoutType
Expand Down Expand Up @@ -855,8 +855,8 @@ - (void)exchangeIndicatorSizeToBig:(BOOL)big
[self.ringLayer removeFromSuperlayer];
[self.backgroundRingLayer removeFromSuperlayer];

self.ringLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDForeGroundColor];
self.backgroundRingLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDBackGroundColor];
self.ringLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDForeGroundDefaultColor()];
self.backgroundRingLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDBackGroundDefaultColor()];
self.ringLayer.strokeEnd = 0;
self.backgroundRingLayer.strokeEnd = 1;
[self.hudView.layer addSublayer:self.backgroundRingLayer];
Expand Down Expand Up @@ -1031,23 +1031,6 @@ - (void)setMaskEdgeWithType: (WSProgressHUDMaskType)maskType
}
}



- (UIImage *)image:(UIImage *)image withTintColor:(UIColor *)color{
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
CGContextRef c = UIGraphicsGetCurrentContext();
[image drawInRect:rect];
CGContextSetFillColorWithColor(c, [color CGColor]);
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
CGContextFillRect(c, rect);
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return tintedImage;
}


- (void)invalidateTimer
{
if (self.timer) {
Expand Down Expand Up @@ -1243,19 +1226,6 @@ - (instancetype)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self) {

WSProgressHUDForeGroundColor = [UIColor whiteColor];
WSProgressHUDBackGroundColor = [UIColor colorWithWhite:0.3 alpha:1];

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSURL *bundleUrl = [bundle URLForResource:@"WSProgressBundle" withExtension:@"bundle"];
NSBundle *imageBundle = [NSBundle bundleWithURL:bundleUrl];

UIImage *successImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"success@2x" ofType:@"png"]];
UIImage *failurImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"error@2x" ofType:@"png"]];

WSProgressHUDSuccessImage = [self image:successImage withTintColor:WSProgressHUDForeGroundColor];
WSProgressHUDErrorImage = [self image:failurImage withTintColor:WSProgressHUDForeGroundColor];

[self addSubview:self.hudView];

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

#pragma mark - INLine Utils Method

CG_INLINE UIColor * WSProgressHUDForeGroundDefaultColor()
{
if (WSProgressHUDForeGroundColor == nil) {
WSProgressHUDForeGroundColor = [UIColor whiteColor];
}
return WSProgressHUDForeGroundColor;
}

CG_INLINE UIColor * WSProgressHUDBackGroundDefaultColor()
{
if (WSProgressHUDBackGroundColor == nil) {
WSProgressHUDBackGroundColor = [UIColor colorWithWhite:0.3 alpha:1];
}
return WSProgressHUDBackGroundColor;
}


CG_INLINE UIImage * WSProgressHUDImageWithName(NSString *imageName, NSString *imageType)
{
NSBundle *bundle = [NSBundle bundleForClass:[WSProgressHUD class]];
NSURL *bundleUrl = [bundle URLForResource:@"WSProgressBundle" withExtension:@"bundle"];
NSBundle *defaultBundle = [NSBundle bundleWithURL:bundleUrl];
return [UIImage imageWithContentsOfFile:[defaultBundle pathForResource:imageName ofType:imageType]];
}

CG_INLINE UIImage * WSProgressHUDSuccessDefaultImage()
{
if (WSProgressHUDSuccessImage == nil) {
UIImage *successImage = WSProgressHUDImageWithName(@"success@2x", @"png");
WSProgressHUDSuccessImage = WSImageByAddTintColr(successImage, WSProgressHUDForeGroundDefaultColor());
}
return WSProgressHUDSuccessImage;
}

CG_INLINE UIImage * WSProgressHUDErrorDefaultImage()
{
if (WSProgressHUDErrorImage == nil) {
UIImage *failurImage = WSProgressHUDImageWithName(@"error@2x", @"png");
WSProgressHUDErrorImage = WSImageByAddTintColr(failurImage, WSProgressHUDForeGroundDefaultColor());
}
return WSProgressHUDErrorImage;
}


CG_INLINE UIImage * WSImageByAddTintColr(UIImage *image, UIColor *color)
{
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
CGContextRef c = UIGraphicsGetCurrentContext();
[image drawInRect:rect];
CGContextSetFillColorWithColor(c, [color CGColor]);
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
CGContextFillRect(c, rect);
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}

- (UIView *)hudView
{
Expand Down Expand Up @@ -1373,7 +1402,7 @@ - (WSIndefiniteAnimationView *)indefiniteAnimationView
{
if (!_indefiniteAnimationView) {
_indefiniteAnimationView = [[WSIndefiniteAnimationView alloc] initWithFrame:CGRectZero];
_indefiniteAnimationView.strokeColor = WSProgressHUDForeGroundColor;
_indefiniteAnimationView.strokeColor = WSProgressHUDForeGroundDefaultColor();
_indefiniteAnimationView.strokeThickness = WSProgressHUDRingThickness;
_indefiniteAnimationView.radius = 10;
[_indefiniteAnimationView sizeToFit];
Expand Down
103 changes: 66 additions & 37 deletions WSProgressHUD/WSProgressHUD.m
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ + (void)showWithStatus: (NSString *)string maskType: (WSProgressHUDMaskType)mask

+ (void)showSuccessWithStatus: (NSString *)string
{
[self showImage:WSProgressHUDSuccessImage status:string];
[self showImage:WSProgressHUDSuccessDefaultImage() status:string];
}

+ (void)showErrorWithStatus: (NSString *)string
{
[self showImage:WSProgressHUDErrorImage status:string];
[self showImage:WSProgressHUDErrorDefaultImage() status:string];
}


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

- (void)showSuccessWithString: (NSString *)string
{
[self showImage:WSProgressHUDSuccessImage status:string];
[self showImage:WSProgressHUDSuccessDefaultImage() status:string];
}
- (void)showErrorWithString: (NSString *)string
{
[self showImage:WSProgressHUDErrorImage status:string];
[self showImage:WSProgressHUDErrorDefaultImage() status:string];
}

- (void)showWithMaskType: (WSProgressHUDMaskType)maskType maskWithout: (WSProgressHUDMaskWithoutType)withoutType
Expand Down Expand Up @@ -855,8 +855,8 @@ - (void)exchangeIndicatorSizeToBig:(BOOL)big
[self.ringLayer removeFromSuperlayer];
[self.backgroundRingLayer removeFromSuperlayer];

self.ringLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDForeGroundColor];
self.backgroundRingLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDBackGroundColor];
self.ringLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDForeGroundDefaultColor()];
self.backgroundRingLayer = [self createRingLayerWithCenter:center radius:size/2 lineWidth:1 color:WSProgressHUDBackGroundDefaultColor()];
self.ringLayer.strokeEnd = 0;
self.backgroundRingLayer.strokeEnd = 1;
[self.hudView.layer addSublayer:self.backgroundRingLayer];
Expand Down Expand Up @@ -1031,23 +1031,6 @@ - (void)setMaskEdgeWithType: (WSProgressHUDMaskType)maskType
}
}



- (UIImage *)image:(UIImage *)image withTintColor:(UIColor *)color{
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
CGContextRef c = UIGraphicsGetCurrentContext();
[image drawInRect:rect];
CGContextSetFillColorWithColor(c, [color CGColor]);
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
CGContextFillRect(c, rect);
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return tintedImage;
}


- (void)invalidateTimer
{
if (self.timer) {
Expand Down Expand Up @@ -1243,19 +1226,6 @@ - (instancetype)initWithFrame:(CGRect)frame
self = [super initWithFrame:frame];
if (self) {

WSProgressHUDForeGroundColor = [UIColor whiteColor];
WSProgressHUDBackGroundColor = [UIColor colorWithWhite:0.3 alpha:1];

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSURL *bundleUrl = [bundle URLForResource:@"WSProgressBundle" withExtension:@"bundle"];
NSBundle *imageBundle = [NSBundle bundleWithURL:bundleUrl];

UIImage *successImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"success@2x" ofType:@"png"]];
UIImage *failurImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"error@2x" ofType:@"png"]];

WSProgressHUDSuccessImage = [self image:successImage withTintColor:WSProgressHUDForeGroundColor];
WSProgressHUDErrorImage = [self image:failurImage withTintColor:WSProgressHUDForeGroundColor];

[self addSubview:self.hudView];

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

#pragma mark - INLine Utils Method

CG_INLINE UIColor * WSProgressHUDForeGroundDefaultColor()
{
if (WSProgressHUDForeGroundColor == nil) {
WSProgressHUDForeGroundColor = [UIColor whiteColor];
}
return WSProgressHUDForeGroundColor;
}

CG_INLINE UIColor * WSProgressHUDBackGroundDefaultColor()
{
if (WSProgressHUDBackGroundColor == nil) {
WSProgressHUDBackGroundColor = [UIColor colorWithWhite:0.3 alpha:1];
}
return WSProgressHUDBackGroundColor;
}


CG_INLINE UIImage * WSProgressHUDImageWithName(NSString *imageName, NSString *imageType)
{
NSBundle *bundle = [NSBundle bundleForClass:[WSProgressHUD class]];
NSURL *bundleUrl = [bundle URLForResource:@"WSProgressBundle" withExtension:@"bundle"];
NSBundle *defaultBundle = [NSBundle bundleWithURL:bundleUrl];
return [UIImage imageWithContentsOfFile:[defaultBundle pathForResource:imageName ofType:imageType]];
}

CG_INLINE UIImage * WSProgressHUDSuccessDefaultImage()
{
if (WSProgressHUDSuccessImage == nil) {
UIImage *successImage = WSProgressHUDImageWithName(@"success@2x", @"png");
WSProgressHUDSuccessImage = WSImageByAddTintColr(successImage, WSProgressHUDForeGroundDefaultColor());
}
return WSProgressHUDSuccessImage;
}

CG_INLINE UIImage * WSProgressHUDErrorDefaultImage()
{
if (WSProgressHUDErrorImage == nil) {
UIImage *failurImage = WSProgressHUDImageWithName(@"error@2x", @"png");
WSProgressHUDErrorImage = WSImageByAddTintColr(failurImage, WSProgressHUDForeGroundDefaultColor());
}
return WSProgressHUDErrorImage;
}


CG_INLINE UIImage * WSImageByAddTintColr(UIImage *image, UIColor *color)
{
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
CGContextRef c = UIGraphicsGetCurrentContext();
[image drawInRect:rect];
CGContextSetFillColorWithColor(c, [color CGColor]);
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
CGContextFillRect(c, rect);
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}

- (UIView *)hudView
{
Expand Down Expand Up @@ -1373,7 +1402,7 @@ - (WSIndefiniteAnimationView *)indefiniteAnimationView
{
if (!_indefiniteAnimationView) {
_indefiniteAnimationView = [[WSIndefiniteAnimationView alloc] initWithFrame:CGRectZero];
_indefiniteAnimationView.strokeColor = WSProgressHUDForeGroundColor;
_indefiniteAnimationView.strokeColor = WSProgressHUDForeGroundDefaultColor();
_indefiniteAnimationView.strokeThickness = WSProgressHUDRingThickness;
_indefiniteAnimationView.radius = 10;
[_indefiniteAnimationView sizeToFit];
Expand Down

0 comments on commit 658b914

Please sign in to comment.