mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
Support bundle argument for image loading
Reviewed By: mmmulani Differential Revision: D3768798 fbshipit-source-id: 5b35f06957cebfe74aca90fe6a456f7f739509a9
This commit is contained in:
committed by
Facebook Github Bot 3
parent
74308209f9
commit
46b54fd7a8
@@ -21,6 +21,12 @@ const ImageURISourcePropType = PropTypes.shape({
|
||||
* function).
|
||||
*/
|
||||
uri: PropTypes.string,
|
||||
/**
|
||||
* `bundle` is the iOS asset bundle which the image is included in. This
|
||||
* will default to [NSBundle mainBundle] if not set.
|
||||
* @platform ios
|
||||
*/
|
||||
bundle: PropTypes.string,
|
||||
/**
|
||||
* `method` is the HTTP Method to use. Defaults to GET if not specified.
|
||||
*/
|
||||
|
||||
@@ -36,6 +36,36 @@ 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
|
||||
@@ -50,7 +80,23 @@ RCT_EXPORT_MODULE()
|
||||
}
|
||||
|
||||
NSString *imageName = RCTBundlePathForURL(imageURL);
|
||||
UIImage *image = [UIImage imageNamed:imageName];
|
||||
|
||||
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 (progressHandler) {
|
||||
progressHandler(1, 1);
|
||||
|
||||
@@ -34,7 +34,7 @@ RCT_EXPORT_MODULE()
|
||||
}
|
||||
|
||||
- (NSOperation *)sendRequest:(NSURLRequest *)request
|
||||
withDelegate:(id<RCTURLRequestDelegate>)delegate
|
||||
withDelegate:(id<RCTURLRequestDelegate>)delegate
|
||||
{
|
||||
// Lazy setup
|
||||
if (!_fileQueue) {
|
||||
|
||||
Reference in New Issue
Block a user