iOS: implements base64 for placeholder & error in image

This commit is contained in:
王劲鹏 2020-05-11 11:29:04 +08:00 committed by osborn
parent 39401524f2
commit 1830acf977

View File

@ -37,8 +37,10 @@ @interface DoricImageNode ()
@property(nonatomic, assign) UIViewContentMode contentMode; @property(nonatomic, assign) UIViewContentMode contentMode;
@property(nonatomic, strong) NSNumber *placeHolderColor; @property(nonatomic, strong) NSNumber *placeHolderColor;
@property(nonatomic, strong) NSString *placeHolderImage; @property(nonatomic, strong) NSString *placeHolderImage;
@property(nonatomic, strong) NSString *placeHolderImageBase64;
@property(nonatomic, strong) NSNumber *errorColor; @property(nonatomic, strong) NSNumber *errorColor;
@property(nonatomic, strong) NSString *errorImage; @property(nonatomic, strong) NSString *errorImage;
@property(nonatomic, strong) NSString *errorImageBase64;
@property(nonatomic, strong) UIVisualEffectView *blurEffectView; @property(nonatomic, strong) UIVisualEffectView *blurEffectView;
@property(nonatomic, strong) NSDictionary *stretchInsetDic; @property(nonatomic, strong) NSDictionary *stretchInsetDic;
@property(nonatomic, assign) CGFloat imageScale; @property(nonatomic, assign) CGFloat imageScale;
@ -61,12 +63,18 @@ - (void)blend:(NSDictionary *)props {
[props[@"placeHolderImage"] also:^(id it) { [props[@"placeHolderImage"] also:^(id it) {
self.placeHolderImage = it; self.placeHolderImage = it;
}]; }];
[props[@"placeHolderImageBase64"] also:^(id it) {
self.placeHolderImageBase64 = it;
}];
[props[@"errorColor"] also:^(id it) { [props[@"errorColor"] also:^(id it) {
self.errorColor = it; self.errorColor = it;
}]; }];
[props[@"errorImage"] also:^(id it) { [props[@"errorImage"] also:^(id it) {
self.errorImage = it; self.errorImage = it;
}]; }];
[props[@"errorImageBase64"] also:^(id it) {
self.errorImageBase64 = it;
}];
[props[@"imageScale"] also:^(NSNumber *it) { [props[@"imageScale"] also:^(NSNumber *it) {
self.imageScale = it.floatValue; self.imageScale = it.floatValue;
}]; }];
@ -77,6 +85,18 @@ - (UIImage *)currentPlaceHolderImage {
if (self.placeHolderImage) { if (self.placeHolderImage) {
return [UIImage imageNamed:self.placeHolderImage]; return [UIImage imageNamed:self.placeHolderImage];
} }
if (self.placeHolderImageBase64) {
NSString *base64 = self.placeHolderImageBase64;
if (YES == [base64 hasPrefix:@"data:image"]) {
base64 = [base64 componentsSeparatedByString:@","].lastObject;
}
NSData *imageData = [[NSData alloc] initWithBase64EncodedString:base64
options:NSDataBase64DecodingIgnoreUnknownCharacters];
YYImage *image = [YYImage imageWithData:imageData scale:self.imageScale];
return image;
}
if (self.placeHolderColor) { if (self.placeHolderColor) {
UIColor *color = DoricColor(self.placeHolderColor); UIColor *color = DoricColor(self.placeHolderColor);
CGRect rect = CGRectMake(0, 0, 1, 1); CGRect rect = CGRectMake(0, 0, 1, 1);
@ -98,6 +118,18 @@ - (UIImage *)currentErrorImage {
if (self.errorImage) { if (self.errorImage) {
return [UIImage imageNamed:self.errorImage]; return [UIImage imageNamed:self.errorImage];
} }
if (self.errorImageBase64) {
NSString *base64 = self.errorImageBase64;
if (YES == [base64 hasPrefix:@"data:image"]) {
base64 = [base64 componentsSeparatedByString:@","].lastObject;
}
NSData *imageData = [[NSData alloc] initWithBase64EncodedString:base64
options:NSDataBase64DecodingIgnoreUnknownCharacters];
YYImage *image = [YYImage imageWithData:imageData scale:self.imageScale];
return image;
}
if (self.errorColor) { if (self.errorColor) {
UIColor *color = DoricColor(self.errorColor); UIColor *color = DoricColor(self.errorColor);
CGRect rect = CGRectMake(0, 0, 1, 1); CGRect rect = CGRectMake(0, 0, 1, 1);