Support bundle argument for image loading

Reviewed By: mmmulani

Differential Revision: D3768798

fbshipit-source-id: 5b35f06957cebfe74aca90fe6a456f7f739509a9
This commit is contained in:
Pieter De Baets
2016-08-31 17:30:50 -07:00
committed by Facebook Github Bot 3
parent 74308209f9
commit 46b54fd7a8
14 changed files with 286 additions and 7 deletions

View File

@@ -36,6 +36,36 @@ RCT_EXPORT_MODULE()
return NO;
}
static NSString *bundleName(NSBundle *bundle)
{
NSString *name = bundle.infoDictionary[@"CFBundleName"];
if (!name) {
name = [[bundle.bundlePath lastPathComponent] stringByDeletingPathExtension];
}
return name;
}
static NSBundle *bundleForPath(NSString *key)
{
static NSMutableDictionary *bundleCache;
if (!bundleCache) {
bundleCache = [NSMutableDictionary new];
bundleCache[@"main"] = [NSBundle mainBundle];
// Initialize every bundle in the array
for (NSString *path in [[NSBundle mainBundle] pathsForResourcesOfType:@"bundle" inDirectory:nil]) {
[NSBundle bundleWithPath:path];
}
// The bundles initialized above will now also be in `allBundles`
for (NSBundle *bundle in [NSBundle allBundles]) {
bundleCache[bundleName(bundle)] = bundle;
}
}
return bundleCache[key];
}
- (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
size:(CGSize)size
scale:(CGFloat)scale
@@ -50,7 +80,23 @@ RCT_EXPORT_MODULE()
}
NSString *imageName = RCTBundlePathForURL(imageURL);
UIImage *image = [UIImage imageNamed:imageName];
NSBundle *bundle;
NSArray *imagePathComponents = [imageName pathComponents];
if ([imagePathComponents count] > 1 &&
[[[imagePathComponents firstObject] pathExtension] isEqualToString:@"bundle"]) {
NSString *bundlePath = [imagePathComponents firstObject];
bundle = bundleForPath([bundlePath stringByDeletingPathExtension]);
imageName = [imageName substringFromIndex:(bundlePath.length + 1)];
}
UIImage *image;
if (bundle) {
image = [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
} else {
image = [UIImage imageNamed:imageName];
}
if (image) {
if (progressHandler) {
progressHandler(1, 1);