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.
|
|
|
|
*/
|
|
|
|
|
2015-10-12 11:14:21 +00:00
|
|
|
#import "RCTXCAssetImageLoader.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"
|
|
|
|
|
2015-10-12 11:14:21 +00:00
|
|
|
@implementation RCTXCAssetImageLoader
|
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
|
|
|
{
|
2015-10-12 11:14:21 +00:00
|
|
|
return RCTIsXCAssetURL(requestURL);
|
2015-09-02 15:25:10 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 12:00:50 +00:00
|
|
|
- (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
|
|
|
|
size:(CGSize)size
|
|
|
|
scale:(CGFloat)scale
|
|
|
|
resizeMode:(UIViewContentMode)resizeMode
|
|
|
|
progressHandler:(RCTImageLoaderProgressBlock)progressHandler
|
|
|
|
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;
|
2015-09-02 15:25:10 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2015-10-20 12:00:50 +00:00
|
|
|
|
2015-09-02 15:25:10 +00:00
|
|
|
if (cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-12 11:14:21 +00:00
|
|
|
NSString *imageName = RCTBundlePathForURL(imageURL);
|
2015-09-02 15:25:10 +00:00
|
|
|
UIImage *image = [UIImage imageNamed:imageName];
|
|
|
|
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
|