Expose RCTImageLocalAssetURL as a utility
Reviewed By: javache Differential Revision: D4696627 fbshipit-source-id: 56d3e59983f524dfd5021835734b9b34203e20f2
This commit is contained in:
parent
11814a5a81
commit
f48b54bf62
|
@ -36,36 +36,6 @@ RCT_EXPORT_MODULE()
|
||||||
return NO;
|
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
|
- (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
|
||||||
size:(CGSize)size
|
size:(CGSize)size
|
||||||
scale:(CGFloat)scale
|
scale:(CGFloat)scale
|
||||||
|
@ -80,31 +50,14 @@ static NSBundle *bundleForPath(NSString *key)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSString *imageName = RCTBundlePathForURL(imageURL);
|
UIImage *image = RCTImageFromLocalAssetURL(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];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (image) {
|
if (image) {
|
||||||
if (progressHandler) {
|
if (progressHandler) {
|
||||||
progressHandler(1, 1);
|
progressHandler(1, 1);
|
||||||
}
|
}
|
||||||
completionHandler(nil, image);
|
completionHandler(nil, image);
|
||||||
} else {
|
} else {
|
||||||
NSString *message = [NSString stringWithFormat:@"Could not find image named %@", imageName];
|
NSString *message = [NSString stringWithFormat:@"Could not find image %@", imageURL];
|
||||||
RCTLogWarn(@"%@", message);
|
RCTLogWarn(@"%@", message);
|
||||||
completionHandler(RCTErrorWithMessage(message), nil);
|
completionHandler(RCTErrorWithMessage(message), nil);
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,6 +114,10 @@ RCT_EXTERN NSString *__nullable RCTBundlePathForURL(NSURL *__nullable URL);
|
||||||
// Determines if a given image URL refers to a local image
|
// Determines if a given image URL refers to a local image
|
||||||
RCT_EXTERN BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL);
|
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
|
// Creates a new, unique temporary file path with the specified extension
|
||||||
RCT_EXTERN NSString *__nullable RCTTempFilePath(NSString *__nullable extension, NSError **error);
|
RCT_EXTERN NSString *__nullable RCTTempFilePath(NSString *__nullable extension, NSError **error);
|
||||||
|
|
||||||
|
|
|
@ -599,6 +599,60 @@ BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL)
|
||||||
return [extension isEqualToString:@"png"] || [extension isEqualToString:@"jpg"];
|
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)
|
RCT_EXTERN NSString *__nullable RCTTempFilePath(NSString *extension, NSError **error)
|
||||||
{
|
{
|
||||||
static NSError *setupError = nil;
|
static NSError *setupError = nil;
|
||||||
|
|
Loading…
Reference in New Issue