[Image] Add scale support for base64-encoded image.

Summary:
Fix issue #1136
Closes https://github.com/facebook/react-native/pull/1721
Github Author: =?UTF-8?q?=E9=9A=90=E9=A3=8E?= <yinfeng.fcx@alibaba-inc.com>

Test Plan: Imported from GitHub, without a `Test Plan:` line.
This commit is contained in:
=?UTF-8?q?=E9=9A=90=E9=A3=8E?= 2015-06-25 06:00:02 -07:00
parent f23c022f1b
commit 7fc86dded3
4 changed files with 21 additions and 9 deletions

View File

@ -635,17 +635,24 @@ RCT_CGSTRUCT_CONVERTER(CGAffineTransform, (@[
return nil; return nil;
} }
if (RCT_DEBUG && ![json isKindOfClass:[NSString class]]) { if (RCT_DEBUG && ![json isKindOfClass:[NSString class]] && ![json isKindOfClass:[NSDictionary class]]) {
RCTLogConvertError(json, "an image"); RCTLogConvertError(json, "an image");
return nil; return nil;
} }
if ([json length] == 0) { UIImage *image;
return nil; NSString *path;
CGFloat scale = 0.0;
if ([json isKindOfClass:[NSString class]]) {
if ([json length] == 0) {
return nil;
}
path = json;
} else {
path = [self NSString:json[@"uri"]];
scale = [self CGFloat:json[@"scale"]];
} }
UIImage *image = nil;
NSString *path = json;
if ([path hasPrefix:@"data:"]) { if ([path hasPrefix:@"data:"]) {
NSURL *url = [NSURL URLWithString:path]; NSURL *url = [NSURL URLWithString:path];
NSData *imageData = [NSData dataWithContentsOfURL:url]; NSData *imageData = [NSData dataWithContentsOfURL:url];
@ -658,6 +665,11 @@ RCT_CGSTRUCT_CONVERTER(CGAffineTransform, (@[
image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:path ofType:nil]]; image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:path ofType:nil]];
} }
} }
if (scale > 0) {
image = [UIImage imageWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation];
}
// NOTE: we don't warn about nil images because there are legitimate // NOTE: we don't warn about nil images because there are legitimate
// case where we find out if a string is an image by using this method // case where we find out if a string is an image by using this method
return image; return image;

View File

@ -11,7 +11,7 @@
@interface RCTTabBarItem : UIView @interface RCTTabBarItem : UIView
@property (nonatomic, copy) NSString *icon; @property (nonatomic, copy) id icon;
@property (nonatomic, assign, getter=isSelected) BOOL selected; @property (nonatomic, assign, getter=isSelected) BOOL selected;
@property (nonatomic, readonly) UITabBarItem *barItem; @property (nonatomic, readonly) UITabBarItem *barItem;

View File

@ -25,7 +25,7 @@
return _barItem; return _barItem;
} }
- (void)setIcon:(NSString *)icon - (void)setIcon:(id)icon
{ {
static NSDictionary *systemIcons; static NSDictionary *systemIcons;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
@ -54,7 +54,7 @@
UIImage *image = [RCTConvert UIImage:_icon]; UIImage *image = [RCTConvert UIImage:_icon];
UITabBarItem *oldItem = _barItem; UITabBarItem *oldItem = _barItem;
if (image) { if (image) {
// Recreate barItem if previous item was a system icon // Recreate barItem if previous item was a system icon
if (wasSystemIcon) { if (wasSystemIcon) {
_barItem = nil; _barItem = nil;

View File

@ -22,7 +22,7 @@ RCT_EXPORT_MODULE()
} }
RCT_EXPORT_VIEW_PROPERTY(selected, BOOL); RCT_EXPORT_VIEW_PROPERTY(selected, BOOL);
RCT_EXPORT_VIEW_PROPERTY(icon, NSString); RCT_EXPORT_VIEW_PROPERTY(icon, id);
RCT_REMAP_VIEW_PROPERTY(selectedIcon, barItem.selectedImage, UIImage); RCT_REMAP_VIEW_PROPERTY(selectedIcon, barItem.selectedImage, UIImage);
RCT_REMAP_VIEW_PROPERTY(badge, barItem.badgeValue, NSString); RCT_REMAP_VIEW_PROPERTY(badge, barItem.badgeValue, NSString);
RCT_CUSTOM_VIEW_PROPERTY(title, NSString, RCTTabBarItem) RCT_CUSTOM_VIEW_PROPERTY(title, NSString, RCTTabBarItem)