Permalink
Browse files

Expose RCTImageLocalAssetURL as a utility

Reviewed By: javache

Differential Revision: D4696627

fbshipit-source-id: 56d3e59983f524dfd5021835734b9b34203e20f2
  • Loading branch information...
ashwinb authored and facebook-github-bot committed Mar 17, 2017
1 parent 11814a5 commit f48b54bf6211dbbdd32839b0de68b1d0a451e486
Showing with 60 additions and 49 deletions.
  1. +2 −49 Libraries/Image/RCTLocalAssetImageLoader.m
  2. +4 −0 React/Base/RCTUtils.h
  3. +54 −0 React/Base/RCTUtils.m
@@ -36,36 +36,6 @@ - (BOOL)shouldCacheLoadedImages
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
@@ -80,31 +50,14 @@ - (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
return;
}
NSString *imageName = RCTBundlePathForURL(imageURL);
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];
}
UIImage *image = RCTImageFromLocalAssetURL(imageURL);
if (image) {
if (progressHandler) {
progressHandler(1, 1);
}
completionHandler(nil, image);
} else {
NSString *message = [NSString stringWithFormat:@"Could not find image named %@", imageName];
NSString *message = [NSString stringWithFormat:@"Could not find image %@", imageURL];
RCTLogWarn(@"%@", message);
completionHandler(RCTErrorWithMessage(message), nil);
}
View
@@ -114,6 +114,10 @@ RCT_EXTERN NSString *__nullable RCTBundlePathForURL(NSURL *__nullable URL);
// Determines if a given image URL refers to a local image
RCT_EXTERN BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL);
// Returns an UIImage for a local image asset. Returns nil if the URL
// does not correspond to a local asset.
RCT_EXTERN UIImage *RCTImageFromLocalAssetURL(NSURL *imageURL);
// Creates a new, unique temporary file path with the specified extension
RCT_EXTERN NSString *__nullable RCTTempFilePath(NSString *__nullable extension, NSError **error);
View
@@ -599,6 +599,60 @@ BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL)
return [extension isEqualToString:@"png"] || [extension isEqualToString:@"jpg"];
}
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];
}
UIImage *RCTImageFromLocalAssetURL(NSURL *imageURL)
{
if (!RCTIsLocalAssetURL(imageURL)) {
return nil;
}
NSString *imageName = RCTBundlePathForURL(imageURL);
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)];
}
if (bundle) {
return [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
} else {
return [UIImage imageNamed:imageName];
}
}
RCT_EXTERN NSString *__nullable RCTTempFilePath(NSString *extension, NSError **error)
{
static NSError *setupError = nil;

0 comments on commit f48b54b

Please sign in to comment.