mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-28 12:15:37 +08:00
Unify XCAsset handling logic
Summary: @public This diff unifies the logic for detecting when images refer to XCAsset files into a single function (RCTXCAssetNameForURL) and uses it for both +[RCTConvert UIImage:] and RCTImageLoader. I've also tightened the definition of XCAssets so that it only applies to images inside .car files, not any image inside the main bundle. This avoids using the +[UIImage imageNamed:] when not strictly necessary, which is desirable since that method is not thread-safe, and has undocumented caching behavior that is difficult to reason about. Reviewed By: @javache Differential Revision: D2526400 fb-gh-sync-id: 7199c2a44f1d55ff236d2c38a0a9368739b993d5
This commit is contained in:
committed by
facebook-github-bot-4
parent
c16c3f9814
commit
799168929c
59
Libraries/Image/RCTXCAssetImageLoader.m
Normal file
59
Libraries/Image/RCTXCAssetImageLoader.m
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#import "RCTXCAssetImageLoader.h"
|
||||
|
||||
#import "RCTUtils.h"
|
||||
|
||||
@implementation RCTXCAssetImageLoader
|
||||
|
||||
RCT_EXPORT_MODULE()
|
||||
|
||||
- (BOOL)canLoadImageURL:(NSURL *)requestURL
|
||||
{
|
||||
return RCTIsXCAssetURL(requestURL);
|
||||
}
|
||||
|
||||
- (float)imageLoaderPriority
|
||||
{
|
||||
return 100; // higher priority than any ordinary file loader
|
||||
}
|
||||
|
||||
- (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL size:(CGSize)size scale:(CGFloat)scale resizeMode:(UIViewContentMode)resizeMode progressHandler:(RCTImageLoaderProgressBlock)progressHandler completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
|
||||
{
|
||||
__block BOOL cancelled = NO;
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (cancelled) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *imageName = RCTBundlePathForURL(imageURL);
|
||||
UIImage *image = [UIImage imageNamed:imageName];
|
||||
if (image) {
|
||||
if (progressHandler) {
|
||||
progressHandler(1, 1);
|
||||
}
|
||||
|
||||
if (completionHandler) {
|
||||
completionHandler(nil, image);
|
||||
}
|
||||
} else {
|
||||
if (completionHandler) {
|
||||
NSString *message = [NSString stringWithFormat:@"Could not find image named %@", imageName];
|
||||
completionHandler(RCTErrorWithMessage(message), nil);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return ^{
|
||||
cancelled = YES;
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user