2015-09-02 08:25:10 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-16 18:24:55 -08: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 08:25:10 -07:00
|
|
|
*/
|
|
|
|
|
2016-07-21 07:45:54 -07:00
|
|
|
#import "RCTLocalAssetImageLoader.h"
|
2015-09-02 08:25:10 -07:00
|
|
|
|
2017-08-01 02:48:28 -07:00
|
|
|
#import <stdatomic.h>
|
2015-10-20 05:00:50 -07:00
|
|
|
|
2016-11-23 07:47:52 -08:00
|
|
|
#import <React/RCTUtils.h>
|
2015-09-02 08:25:10 -07:00
|
|
|
|
2016-07-21 07:45:54 -07:00
|
|
|
@implementation RCTLocalAssetImageLoader
|
2015-09-02 08:25:10 -07:00
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-10-12 04:14:21 -07:00
|
|
|
- (BOOL)canLoadImageURL:(NSURL *)requestURL
|
2015-09-02 08:25:10 -07:00
|
|
|
{
|
2016-07-21 07:45:54 -07: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 08:25:10 -07:00
|
|
|
}
|
|
|
|
|
2015-10-20 05:00:50 -07:00
|
|
|
- (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
|
|
|
|
size:(CGSize)size
|
|
|
|
scale:(CGFloat)scale
|
2016-01-20 11:03:22 -08:00
|
|
|
resizeMode:(RCTResizeMode)resizeMode
|
2015-10-20 05:00:50 -07:00
|
|
|
progressHandler:(RCTImageLoaderProgressBlock)progressHandler
|
2016-09-21 12:11:19 -07:00
|
|
|
partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
|
2015-10-20 05:00:50 -07:00
|
|
|
completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
|
2015-09-02 08:25:10 -07:00
|
|
|
{
|
2017-08-01 02:48:28 -07:00
|
|
|
__block atomic_bool cancelled = ATOMIC_VAR_INIT(NO);
|
2016-07-21 07:45:54 -07:00
|
|
|
RCTExecuteOnMainQueue(^{
|
2017-08-01 02:48:28 -07:00
|
|
|
if (atomic_load(&cancelled)) {
|
2015-09-02 08:25:10 -07:00
|
|
|
return;
|
|
|
|
}
|
2016-07-21 07:45:54 -07:00
|
|
|
|
2017-03-17 16:47:58 -07:00
|
|
|
UIImage *image = RCTImageFromLocalAssetURL(imageURL);
|
2015-09-02 08:25:10 -07:00
|
|
|
if (image) {
|
|
|
|
if (progressHandler) {
|
|
|
|
progressHandler(1, 1);
|
|
|
|
}
|
2015-10-20 05:00:50 -07:00
|
|
|
completionHandler(nil, image);
|
2015-09-02 08:25:10 -07:00
|
|
|
} else {
|
2017-03-17 16:47:58 -07:00
|
|
|
NSString *message = [NSString stringWithFormat:@"Could not find image %@", imageURL];
|
2016-12-19 13:54:49 -08:00
|
|
|
RCTLogWarn(@"%@", message);
|
2015-10-20 05:00:50 -07:00
|
|
|
completionHandler(RCTErrorWithMessage(message), nil);
|
2015-09-02 08:25:10 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ^{
|
2017-08-01 02:48:28 -07:00
|
|
|
atomic_store(&cancelled, YES);
|
2015-09-02 08:25:10 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|