From f48b54bf6211dbbdd32839b0de68b1d0a451e486 Mon Sep 17 00:00:00 2001 From: Ashwin Bharambe Date: Fri, 17 Mar 2017 16:47:58 -0700 Subject: [PATCH] Expose RCTImageLocalAssetURL as a utility Reviewed By: javache Differential Revision: D4696627 fbshipit-source-id: 56d3e59983f524dfd5021835734b9b34203e20f2 --- Libraries/Image/RCTLocalAssetImageLoader.m | 51 +------------------- React/Base/RCTUtils.h | 4 ++ React/Base/RCTUtils.m | 54 ++++++++++++++++++++++ 3 files changed, 60 insertions(+), 49 deletions(-) diff --git a/Libraries/Image/RCTLocalAssetImageLoader.m b/Libraries/Image/RCTLocalAssetImageLoader.m index b1bf78c2d..17acd15b8 100644 --- a/Libraries/Image/RCTLocalAssetImageLoader.m +++ b/Libraries/Image/RCTLocalAssetImageLoader.m @@ -36,36 +36,6 @@ 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 @@ -80,31 +50,14 @@ static NSBundle *bundleForPath(NSString *key) 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); } diff --git a/React/Base/RCTUtils.h b/React/Base/RCTUtils.h index a277120e2..14e169ba2 100644 --- a/React/Base/RCTUtils.h +++ b/React/Base/RCTUtils.h @@ -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); diff --git a/React/Base/RCTUtils.m b/React/Base/RCTUtils.m index 9617d0a3a..80ff69ed5 100644 --- a/React/Base/RCTUtils.m +++ b/React/Base/RCTUtils.m @@ -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;