2015-09-02 15:25:10 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
2016-07-21 14:45:54 +00:00
|
|
|
#import "RCTLocalAssetImageLoader.h"
|
2015-09-02 15:25:10 +00:00
|
|
|
|
2015-10-20 12:00:50 +00:00
|
|
|
#import <libkern/OSAtomic.h>
|
|
|
|
|
2015-09-02 15:25:10 +00:00
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
2016-07-21 14:45:54 +00:00
|
|
|
@implementation RCTLocalAssetImageLoader
|
2015-09-02 15:25:10 +00:00
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-10-12 11:14:21 +00:00
|
|
|
- (BOOL)canLoadImageURL:(NSURL *)requestURL
|
2015-09-02 15:25:10 +00:00
|
|
|
{
|
2016-07-21 14:45:54 +00:00
|
|
|
return RCTIsLocalAssetURL(requestURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)requiresScheduling
|
|
|
|
{
|
|
|
|
// Don't schedule this loader on the URL queue so we can load the
|
|
|
|
// local assets synchronously to avoid flickers.
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)shouldCacheLoadedImages
|
|
|
|
{
|
|
|
|
// UIImage imageNamed handles the caching automatically so we don't want
|
|
|
|
// to add it to the image cache.
|
|
|
|
return NO;
|
2015-09-02 15:25:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 00:30:50 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2015-10-20 12:00:50 +00:00
|
|
|
- (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
|
|
|
|
size:(CGSize)size
|
|
|
|
scale:(CGFloat)scale
|
2016-01-20 19:03:22 +00:00
|
|
|
resizeMode:(RCTResizeMode)resizeMode
|
2015-10-20 12:00:50 +00:00
|
|
|
progressHandler:(RCTImageLoaderProgressBlock)progressHandler
|
2016-09-21 19:11:19 +00:00
|
|
|
partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
|
2015-10-20 12:00:50 +00:00
|
|
|
completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
|
2015-09-02 15:25:10 +00:00
|
|
|
{
|
2015-10-20 12:00:50 +00:00
|
|
|
__block volatile uint32_t cancelled = 0;
|
2016-07-21 14:45:54 +00:00
|
|
|
RCTExecuteOnMainQueue(^{
|
2015-09-02 15:25:10 +00:00
|
|
|
if (cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-21 14:45:54 +00:00
|
|
|
|
2015-10-12 11:14:21 +00:00
|
|
|
NSString *imageName = RCTBundlePathForURL(imageURL);
|
2016-09-01 00:30:50 +00:00
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2015-09-02 15:25:10 +00:00
|
|
|
if (image) {
|
|
|
|
if (progressHandler) {
|
|
|
|
progressHandler(1, 1);
|
|
|
|
}
|
2015-10-20 12:00:50 +00:00
|
|
|
completionHandler(nil, image);
|
2015-09-02 15:25:10 +00:00
|
|
|
} else {
|
2015-10-20 12:00:50 +00:00
|
|
|
NSString *message = [NSString stringWithFormat:@"Could not find image named %@", imageName];
|
|
|
|
completionHandler(RCTErrorWithMessage(message), nil);
|
2015-09-02 15:25:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ^{
|
2015-10-20 12:00:50 +00:00
|
|
|
OSAtomicOr32Barrier(1, &cancelled);
|
2015-09-02 15:25:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|