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 коммит произвёл facebook-github-bot-4
Родитель 3b6d029a55
Коммит 9076b71ac3
4 изменённых файлов: 24 добавлений и 28 удалений

Просмотреть файл

@ -36,7 +36,7 @@
} \
#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
TEST_URL(basic, @"http://example.com", @"http://example.com")

Просмотреть файл

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

Просмотреть файл

@ -39,7 +39,8 @@ RCT_EXPORT_MODULE()
// Have to exclude 'file://' from the main bundle, otherwise this would conflict with RCTAssetBundleImageLoader
return
[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;
}

Просмотреть файл

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