2015-09-02 15:25:10 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-09-02 15:25:10 +00:00
|
|
|
*/
|
|
|
|
|
2016-07-21 14:45:54 +00:00
|
|
|
#import "RCTLocalAssetImageLoader.h"
|
2015-09-02 15:25:10 +00:00
|
|
|
|
2017-08-01 09:48:28 +00:00
|
|
|
#import <stdatomic.h>
|
2015-10-20 12:00:50 +00:00
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTUtils.h>
|
2015-09-02 15:25:10 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-08-01 09:48:28 +00:00
|
|
|
__block atomic_bool cancelled = ATOMIC_VAR_INIT(NO);
|
2016-07-21 14:45:54 +00:00
|
|
|
RCTExecuteOnMainQueue(^{
|
2017-08-01 09:48:28 +00:00
|
|
|
if (atomic_load(&cancelled)) {
|
2015-09-02 15:25:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-07-21 14:45:54 +00:00
|
|
|
|
2017-03-17 23:47:58 +00:00
|
|
|
UIImage *image = RCTImageFromLocalAssetURL(imageURL);
|
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 {
|
2017-03-17 23:47:58 +00:00
|
|
|
NSString *message = [NSString stringWithFormat:@"Could not find image %@", imageURL];
|
2016-12-19 21:54:49 +00:00
|
|
|
RCTLogWarn(@"%@", message);
|
2015-10-20 12:00:50 +00:00
|
|
|
completionHandler(RCTErrorWithMessage(message), nil);
|
2015-09-02 15:25:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ^{
|
2017-08-01 09:48:28 +00:00
|
|
|
atomic_store(&cancelled, YES);
|
2015-09-02 15:25:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|