Skip to content

Commit 7fc86dd

Browse files
committed
[Image] Add scale support for base64-encoded image.
Summary: Fix issue #1136 Closes #1721 Github Author: =?UTF-8?q?=E9=9A=90=E9=A3=8E?= <[email protected]> Test Plan: Imported from GitHub, without a `Test Plan:` line.
1 parent f23c022 commit 7fc86dd

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

React/Base/RCTConvert.m

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -635,17 +635,24 @@ + (UIImage *)UIImage:(id)json
635635
return nil;
636636
}
637637

638-
if (RCT_DEBUG && ![json isKindOfClass:[NSString class]]) {
638+
if (RCT_DEBUG && ![json isKindOfClass:[NSString class]] && ![json isKindOfClass:[NSDictionary class]]) {
639639
RCTLogConvertError(json, "an image");
640640
return nil;
641641
}
642642

643-
if ([json length] == 0) {
644-
return nil;
643+
UIImage *image;
644+
NSString *path;
645+
CGFloat scale = 0.0;
646+
if ([json isKindOfClass:[NSString class]]) {
647+
if ([json length] == 0) {
648+
return nil;
649+
}
650+
path = json;
651+
} else {
652+
path = [self NSString:json[@"uri"]];
653+
scale = [self CGFloat:json[@"scale"]];
645654
}
646655

647-
UIImage *image = nil;
648-
NSString *path = json;
649656
if ([path hasPrefix:@"data:"]) {
650657
NSURL *url = [NSURL URLWithString:path];
651658
NSData *imageData = [NSData dataWithContentsOfURL:url];
@@ -658,6 +665,11 @@ + (UIImage *)UIImage:(id)json
658665
image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:path ofType:nil]];
659666
}
660667
}
668+
669+
if (scale > 0) {
670+
image = [UIImage imageWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation];
671+
}
672+
661673
// NOTE: we don't warn about nil images because there are legitimate
662674
// case where we find out if a string is an image by using this method
663675
return image;

React/Views/RCTTabBarItem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@interface RCTTabBarItem : UIView
1313

14-
@property (nonatomic, copy) NSString *icon;
14+
@property (nonatomic, copy) id icon;
1515
@property (nonatomic, assign, getter=isSelected) BOOL selected;
1616
@property (nonatomic, readonly) UITabBarItem *barItem;
1717

React/Views/RCTTabBarItem.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ - (UITabBarItem *)barItem
2525
return _barItem;
2626
}
2727

28-
- (void)setIcon:(NSString *)icon
28+
- (void)setIcon:(id)icon
2929
{
3030
static NSDictionary *systemIcons;
3131
static dispatch_once_t onceToken;
@@ -54,7 +54,7 @@ - (void)setIcon:(NSString *)icon
5454
UIImage *image = [RCTConvert UIImage:_icon];
5555
UITabBarItem *oldItem = _barItem;
5656
if (image) {
57-
57+
5858
// Recreate barItem if previous item was a system icon
5959
if (wasSystemIcon) {
6060
_barItem = nil;

React/Views/RCTTabBarItemManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ - (UIView *)view
2222
}
2323

2424
RCT_EXPORT_VIEW_PROPERTY(selected, BOOL);
25-
RCT_EXPORT_VIEW_PROPERTY(icon, NSString);
25+
RCT_EXPORT_VIEW_PROPERTY(icon, id);
2626
RCT_REMAP_VIEW_PROPERTY(selectedIcon, barItem.selectedImage, UIImage);
2727
RCT_REMAP_VIEW_PROPERTY(badge, barItem.badgeValue, NSString);
2828
RCT_CUSTOM_VIEW_PROPERTY(title, NSString, RCTTabBarItem)

0 commit comments

Comments
 (0)