Reduce +[UIImage imageNamed:] overhead
This commit is contained in:
parent
7750db05e6
commit
1d072076db
|
@ -664,12 +664,29 @@ RCT_CGSTRUCT_CONVERTER(CGAffineTransform, (@[
|
||||||
|
|
||||||
NSURL *URL = [self NSURL:path];
|
NSURL *URL = [self NSURL:path];
|
||||||
NSString *scheme = [URL.scheme lowercaseString];
|
NSString *scheme = [URL.scheme lowercaseString];
|
||||||
if ([scheme isEqualToString:@"file"]) {
|
if (path && [scheme isEqualToString:@"file"]) {
|
||||||
|
if (RCT_DEBUG || [NSThread currentThread] == [NSThread mainThread]) {
|
||||||
if ([NSThread currentThread] == [NSThread mainThread]) {
|
if ([URL.path hasPrefix:[[NSBundle mainBundle] resourcePath]]) {
|
||||||
// Image may reside inside a .car file, in which case we have no choice
|
// Image may reside inside a .car file, in which case we have no choice
|
||||||
// but to use +[UIImage imageNamed] - but this method isn't thread safe
|
// but to use +[UIImage imageNamed] - but this method isn't thread safe
|
||||||
image = [UIImage imageNamed:path];
|
static NSMutableDictionary *XCAssetMap = nil;
|
||||||
|
if (!XCAssetMap) {
|
||||||
|
XCAssetMap = [[NSMutableDictionary alloc] init];
|
||||||
|
}
|
||||||
|
NSNumber *isAsset = XCAssetMap[path];
|
||||||
|
if (!isAsset || isAsset.boolValue) {
|
||||||
|
image = [UIImage imageNamed:path];
|
||||||
|
if (RCT_DEBUG && image) {
|
||||||
|
// If we succeeded in loading the image via imageNamed, and the
|
||||||
|
// method wasn't called on the main thread, that's a coding error
|
||||||
|
RCTAssertMainThread();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!isAsset) {
|
||||||
|
// Avoid calling `+imageNamed` again in future if it's not needed.
|
||||||
|
XCAssetMap[path] = @(image != nil);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!image) {
|
if (!image) {
|
||||||
|
@ -680,13 +697,6 @@ RCT_CGSTRUCT_CONVERTER(CGAffineTransform, (@[
|
||||||
image = [UIImage imageWithContentsOfFile:path];
|
image = [UIImage imageWithContentsOfFile:path];
|
||||||
}
|
}
|
||||||
|
|
||||||
// We won't warn about nil images because there are legitimate cases
|
|
||||||
// where we find out if a string is an image by using this method, but
|
|
||||||
// we do enforce thread-safe API usage with the following check
|
|
||||||
if (RCT_DEBUG && !image && [UIImage imageNamed:path]) {
|
|
||||||
RCTAssertMainThread();
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if ([scheme isEqualToString:@"data"]) {
|
} else if ([scheme isEqualToString:@"data"]) {
|
||||||
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:URL]];
|
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:URL]];
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue