Use bundleForClass instead of mainBundle to find resources

Reviewed By: @jspahrsummers

Differential Revision: D2485109
This commit is contained in:
Pieter De Baets 2015-09-29 10:33:43 -07:00 committed by facebook-github-bot-4
parent 3b6d029a55
commit 9076b71ac3
4 changed files with 24 additions and 28 deletions

View File

@ -36,7 +36,7 @@
} \ } \
#define TEST_BUNDLE_PATH(name, _input, _expectedPath) \ #define TEST_BUNDLE_PATH(name, _input, _expectedPath) \
TEST_PATH(name, _input, [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:_expectedPath]) TEST_PATH(name, _input, [[[NSBundle bundleForClass:[self class]] bundlePath] stringByAppendingPathComponent:_expectedPath])
// Basic tests // Basic tests
TEST_URL(basic, @"http://example.com", @"http://example.com") TEST_URL(basic, @"http://example.com", @"http://example.com")

View File

@ -21,7 +21,7 @@ RCT_EXPORT_MODULE()
return nil; return nil;
} }
NSString *resourcesPath = [NSBundle mainBundle].resourcePath; NSString *resourcesPath = [NSBundle bundleForClass:[self class]].resourcePath;
NSString *requestPath = requestURL.absoluteURL.path; NSString *requestPath = requestURL.absoluteURL.path;
if (requestPath.length < resourcesPath.length + 1) { if (requestPath.length < resourcesPath.length + 1) {
return nil; return nil;
@ -37,8 +37,9 @@ RCT_EXPORT_MODULE()
return NO; return NO;
} }
if ([[NSBundle mainBundle] URLForResource:imageName withExtension:nil] || NSBundle *bundle = [NSBundle bundleForClass:[self class]];
[[NSBundle mainBundle] URLForResource:imageName withExtension:@"png"]) { if ([bundle URLForResource:imageName withExtension:nil] ||
[bundle URLForResource:imageName withExtension:@"png"]) {
return YES; return YES;
} }

View File

@ -39,7 +39,8 @@ RCT_EXPORT_MODULE()
// Have to exclude 'file://' from the main bundle, otherwise this would conflict with RCTAssetBundleImageLoader // Have to exclude 'file://' from the main bundle, otherwise this would conflict with RCTAssetBundleImageLoader
return return
[requestURL.scheme compare:@"http" options:NSCaseInsensitiveSearch range:NSMakeRange(0, 4)] == NSOrderedSame || [requestURL.scheme compare:@"http" options:NSCaseInsensitiveSearch range:NSMakeRange(0, 4)] == NSOrderedSame ||
([requestURL.scheme caseInsensitiveCompare:@"file"] == NSOrderedSame && ![requestURL.path hasPrefix:[NSBundle mainBundle].resourcePath]) || ([requestURL.scheme caseInsensitiveCompare:@"file"] == NSOrderedSame &&
![requestURL.path hasPrefix:[NSBundle bundleForClass:[self class]].resourcePath]) ||
[requestURL.scheme caseInsensitiveCompare:@"data"] == NSOrderedSame; [requestURL.scheme caseInsensitiveCompare:@"data"] == NSOrderedSame;
} }

View File

@ -101,7 +101,7 @@ RCT_CUSTOM_CONVERTER(NSData *, NSData, [json dataUsingEncoding:NSUTF8StringEncod
path = path.stringByExpandingTildeInPath; path = path.stringByExpandingTildeInPath;
} else if (!path.absolutePath) { } else if (!path.absolutePath) {
// Assume it's a resource path // Assume it's a resource path
path = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:path]; path = [[NSBundle bundleForClass:[self class]].resourcePath stringByAppendingPathComponent:path];
} }
return [NSURL fileURLWithPath:path]; return [NSURL fileURLWithPath:path];
} }
@ -426,28 +426,22 @@ RCT_CGSTRUCT_CONVERTER(CGAffineTransform, (@[
NSURL *URL = [self NSURL:path]; NSURL *URL = [self NSURL:path];
NSString *scheme = URL.scheme.lowercaseString; NSString *scheme = URL.scheme.lowercaseString;
if (path && [scheme isEqualToString:@"file"]) { if (URL && [scheme isEqualToString:@"file"]) {
if (RCT_DEBUG || [NSThread currentThread] == [NSThread mainThread]) { RCTAssertMainThread();
if ([URL.path hasPrefix:[NSBundle mainBundle].resourcePath]) { if ([URL.path hasPrefix:[NSBundle bundleForClass:[self class]].resourcePath]) {
// Image may reside inside a .car file, in which case we have no choice // Image may reside inside a .car file, in which case we have no choice
// but to use +[UIImage imageNamed] - but this method isn't thread safe // but to use +[UIImage imageNamed] - but this method isn't thread safe
static NSMutableDictionary *XCAssetMap = nil; static NSMutableDictionary *XCAssetMap = nil;
if (!XCAssetMap) { if (!XCAssetMap) {
XCAssetMap = [NSMutableDictionary new]; XCAssetMap = [NSMutableDictionary new];
} }
NSNumber *isAsset = XCAssetMap[path]; NSNumber *isAsset = XCAssetMap[path];
if (!isAsset || isAsset.boolValue) { if (!isAsset || isAsset.boolValue) {
image = [UIImage imageNamed:path]; image = [UIImage imageNamed:URL.path];
if (RCT_DEBUG && image) { }
// If we succeeded in loading the image via imageNamed, and the if (!isAsset) {
// method wasn't called on the main thread, that's a coding error // Avoid calling `+imageNamed` again in future if it's not needed.
RCTAssertMainThread(); XCAssetMap[path] = @(image != nil);
}
}
if (!isAsset) {
// Avoid calling `+imageNamed` again in future if it's not needed.
XCAssetMap[path] = @(image != nil);
}
} }
} }