Skip to content

Commit

Permalink
Use asset catalog for ios images
Browse files Browse the repository at this point in the history
  • Loading branch information
janicduplessis committed Oct 10, 2020
1 parent 4d4992c commit 072c071
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ package-lock.json
/packages/rn-tester/Pods/*
!/packages/rn-tester/Pods/__offline_mirrors__

# Generated asset catalog
/packages/rn-tester/RNTester/RNAssets.xcassets/*.imageset

# react-native-codegen
/packages/react-native-codegen/lib
/ReactCommon/fabric/components/rncore/
Expand Down
27 changes: 21 additions & 6 deletions Libraries/Image/AssetSourceResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const invariant = require('invariant');

const {
getAndroidResourceFolderName,
getAndroidResourceIdentifier,
getResourceIdentifier,
getBasePath,
} = require('@react-native/assets/path-support');

Expand All @@ -46,9 +46,9 @@ function getScaledAssetPath(asset): string {
*/
function getAssetPathInDrawableFolder(asset): string {
const scale = AssetSourceResolver.pickScale(asset.scales, PixelRatio.get());
const drawbleFolder = getAndroidResourceFolderName(asset, scale);
const fileName = getAndroidResourceIdentifier(asset);
return drawbleFolder + '/' + fileName + '.' + asset.type;
const drawableFolder = getAndroidResourceFolderName(asset, scale);
const fileName = getResourceIdentifier(asset);
return drawableFolder + '/' + fileName + '.' + asset.type;
}

class AssetSourceResolver {
Expand All @@ -72,6 +72,15 @@ class AssetSourceResolver {
return !!(this.jsbundleUrl && this.jsbundleUrl.startsWith('file://'));
}

isCatalogAsset(): boolean {
return (
this.asset.__packager_asset &&
(this.asset.type === 'png' ||
this.asset.type === 'jpg' ||
this.asset.type === 'jpeg')
);
}

defaultAsset(): ResolvedAssetSource {
if (this.isLoadedFromServer()) {
return this.assetServerURL();
Expand All @@ -82,7 +91,9 @@ class AssetSourceResolver {
? this.drawableFolderInBundle()
: this.resourceIdentifierWithoutScale();
} else {
return this.scaledAssetURLNearBundle();
return this.isCatalogAsset()
? this.assetFromCatalog()
: this.scaledAssetURLNearBundle();
}
}

Expand Down Expand Up @@ -124,6 +135,10 @@ class AssetSourceResolver {
);
}

assetFromCatalog(): ResolvedAssetSource {
return this.fromSource(getResourceIdentifier(this.asset));
}

/**
* The default location of assets bundled with the app, located by
* resource identifier
Expand All @@ -135,7 +150,7 @@ class AssetSourceResolver {
Platform.OS === 'android',
'resource identifiers work on Android',
);
return this.fromSource(getAndroidResourceIdentifier(this.asset));
return this.fromSource(getResourceIdentifier(this.asset));
}

/**
Expand Down
5 changes: 4 additions & 1 deletion React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,11 @@ BOOL RCTIsGzippedData(NSData *__nullable data)

static BOOL RCTIsImageAssetsPath(NSString *path)
{
if (path == nil) {
return NO;
}
NSString *extension = [path pathExtension];
return [extension isEqualToString:@"png"] || [extension isEqualToString:@"jpg"];
return extension.length == 0 || [extension isEqualToString:@"png"] || [extension isEqualToString:@"jpg"];
}

BOOL RCTIsBundleAssetURL(NSURL *__nullable imageURL)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"@react-native-community/cli": "^4.13.0",
"@react-native-community/cli-platform-android": "^4.13.0",
"@react-native-community/cli-platform-ios": "^4.13.0",
"@react-native/assets": "1.0.0",
"@react-native/assets": "1.1.0",
"@react-native/normalize-color": "1.0.0",
"@react-native/polyfills": "1.0.0",
"abort-controller": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/assets/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@react-native/assets",
"version": "1.0.0",
"version": "1.1.0",
"description": "Asset support code for React Native.",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions packages/assets/path-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function getAndroidResourceFolderName(
return 'drawable-' + suffix;
}

function getAndroidResourceIdentifier(asset: PackagerAsset): string {
function getResourceIdentifier(asset: PackagerAsset): string {
return (getBasePath(asset) + '/' + asset.name)
.toLowerCase()
.replace(/\//g, '_') // Encode folder structure in file name
Expand All @@ -80,6 +80,6 @@ function getBasePath(asset: PackagerAsset): string {

module.exports = {
getAndroidResourceFolderName,
getAndroidResourceIdentifier,
getResourceIdentifier,
getBasePath,
};
6 changes: 6 additions & 0 deletions packages/rn-tester/RNTester/RNAssets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
9 changes: 7 additions & 2 deletions packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0C49AA40252E529E004CE48B /* RNAssets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0C49AA3F252E529E004CE48B /* RNAssets.xcassets */; };
1317FC3A10A7E894D970D9F2 /* libPods-RNTester.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C2F0C3405B1D6741F52D4F6 /* libPods-RNTester.a */; };
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
272E6B3F1BEA849E001FCF37 /* UpdatePropertiesExampleView.m in Sources */ = {isa = PBXBuildFile; fileRef = 272E6B3C1BEA849E001FCF37 /* UpdatePropertiesExampleView.m */; };
Expand Down Expand Up @@ -70,6 +71,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
0C49AA3F252E529E004CE48B /* RNAssets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = RNAssets.xcassets; path = RNTester/RNAssets.xcassets; sourceTree = "<group>"; };
13B07F961A680F5B00A75B9A /* RNTester.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RNTester.app; sourceTree = BUILT_PRODUCTS_DIR; };
13B07FAF1A68108700A75B9A /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AppDelegate.h; path = RNTester/AppDelegate.h; sourceTree = "<group>"; };
13B07FB61A68108700A75B9A /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = RNTester/Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -164,7 +166,7 @@
E7DB215F22B2F3EC005AC45F /* RCTUIManagerScenarioTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTUIManagerScenarioTests.m; sourceTree = "<group>"; };
E7DB216022B2F3EC005AC45F /* RNTesterSnapshotTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNTesterSnapshotTests.m; sourceTree = "<group>"; };
E7DB216122B2F3EC005AC45F /* RCTRootViewIntegrationTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTRootViewIntegrationTests.m; sourceTree = "<group>"; };
E7DB218B22B41FCD005AC45F /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = XCTest.framework; sourceTree = DEVELOPER_DIR; };
E7DB218B22B41FCD005AC45F /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = XCTest.framework; sourceTree = DEVELOPER_DIR; };
E9618482EC8608D4872A6E28 /* Pods-RNTesterUnitTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RNTesterUnitTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-RNTesterUnitTests/Pods-RNTesterUnitTests.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -219,6 +221,7 @@
5CB07C99226467E60039471C /* RNTesterTurboModuleProvider.mm */,
13B07FB71A68108700A75B9A /* main.m */,
2DDEF00F1F84BF7B00DBDF73 /* Images.xcassets */,
0C49AA3F252E529E004CE48B /* RNAssets.xcassets */,
8145AE05241172D900A3F8DA /* LaunchScreen.storyboard */,
680759612239798500290469 /* Fabric */,
272E6B3A1BEA846C001FCF37 /* NativeExampleViews */,
Expand Down Expand Up @@ -395,8 +398,8 @@
F9CB97B0D9633939D43E75E0 /* [CP] Check Pods Manifest.lock */,
13B07F871A680F5B00A75B9A /* Sources */,
13B07F8C1A680F5B00A75B9A /* Frameworks */,
13B07F8E1A680F5B00A75B9A /* Resources */,
68CD48B71D2BCB2C007E06A9 /* Build JS Bundle */,
13B07F8E1A680F5B00A75B9A /* Resources */,
5CF0FD27207FC6EC00C13D65 /* Start Metro */,
C1C7A9D58CE1D09515F20577 /* [CP] Copy Pods Resources */,
);
Expand Down Expand Up @@ -491,6 +494,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0C49AA40252E529E004CE48B /* RNAssets.xcassets in Resources */,
2DDEF0101F84BF7B00DBDF73 /* Images.xcassets in Resources */,
8145AE06241172D900A3F8DA /* LaunchScreen.storyboard in Resources */,
3D2AFAF51D646CF80089D1A3 /* legacy_image@2x.png in Resources */,
Expand Down Expand Up @@ -947,6 +951,7 @@
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MTL_ENABLE_DEBUG_INFO = NO;
ONLY_ACTIVE_ARCH = NO;
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
"-DFOLLY_NO_CONFIG",
Expand Down
5 changes: 5 additions & 0 deletions scripts/react-native-xcode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ if [[ $EMIT_SOURCEMAP == true ]]; then
EXTRA_ARGS="$EXTRA_ARGS --sourcemap-output $PACKAGER_SOURCEMAP_FILE"
fi

# PRODUCT_SETTINGS_PATH is where the target Info.plist file is. The asset
# catalog will be in the same folder.
ASSET_CATALOG_DEST=${ASSET_CATALOG_DEST:-"$(dirname "$PRODUCT_SETTINGS_PATH")"}

"$NODE_BINARY" $NODE_ARGS "$CLI_PATH" $BUNDLE_COMMAND \
$CONFIG_ARG \
--entry-file "$ENTRY_FILE" \
Expand All @@ -164,6 +168,7 @@ fi
--reset-cache \
--bundle-output "$BUNDLE_FILE" \
--assets-dest "$DEST" \
--asset-catalog-dest "$ASSET_CATALOG_DEST" \
$EXTRA_ARGS \
$EXTRA_PACKAGER_ARGS

Expand Down
3 changes: 3 additions & 0 deletions template/_gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ buck-out/

# CocoaPods
/ios/Pods/

# Generated asset catalog
**/RNAssets.xcassets/*.imageset
7 changes: 5 additions & 2 deletions template/ios/HelloWorld.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
00E356F31AD99517003FC87E /* HelloWorldTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 00E356F21AD99517003FC87E /* HelloWorldTests.m */; };
0C49AA4A252E5346004CE48B /* RNAssets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 0C49AA49252E5346004CE48B /* RNAssets.xcassets */; };
13B07FBC1A68108700A75B9A /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB01A68108700A75B9A /* AppDelegate.m */; };
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
Expand All @@ -29,6 +30,7 @@
00E356EE1AD99517003FC87E /* HelloWorldTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = HelloWorldTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
00E356F11AD99517003FC87E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
00E356F21AD99517003FC87E /* HelloWorldTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HelloWorldTests.m; sourceTree = "<group>"; };
0C49AA49252E5346004CE48B /* RNAssets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = RNAssets.xcassets; path = HelloWorld/RNAssets.xcassets; sourceTree = "<group>"; };
13B07F961A680F5B00A75B9A /* HelloWorld.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HelloWorld.app; sourceTree = BUILT_PRODUCTS_DIR; };
13B07FAF1A68108700A75B9A /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AppDelegate.h; path = HelloWorld/AppDelegate.h; sourceTree = "<group>"; };
13B07FB01A68108700A75B9A /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AppDelegate.m; path = HelloWorld/AppDelegate.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -81,6 +83,7 @@
13B07FAF1A68108700A75B9A /* AppDelegate.h */,
13B07FB01A68108700A75B9A /* AppDelegate.m */,
13B07FB51A68108700A75B9A /* Images.xcassets */,
0C49AA49252E5346004CE48B /* RNAssets.xcassets */,
13B07FB61A68108700A75B9A /* Info.plist */,
81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */,
13B07FB71A68108700A75B9A /* main.m */,
Expand All @@ -92,7 +95,6 @@
isa = PBXGroup;
children = (
ED297162215061F000B7C4FE /* JavaScriptCore.framework */,
ED2971642150620600B7C4FE /* JavaScriptCore.framework */,
);
name = Frameworks;
sourceTree = "<group>";
Expand Down Expand Up @@ -155,8 +157,8 @@
FD10A7F022414F080027D42C /* Start Packager */,
13B07F871A680F5B00A75B9A /* Sources */,
13B07F8C1A680F5B00A75B9A /* Frameworks */,
13B07F8E1A680F5B00A75B9A /* Resources */,
00DD1BFF1BD5951E006B06BC /* Bundle React Native code and images */,
13B07F8E1A680F5B00A75B9A /* Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -216,6 +218,7 @@
buildActionMask = 2147483647;
files = (
81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */,
0C49AA4A252E5346004CE48B /* RNAssets.xcassets in Resources */,
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
6 changes: 6 additions & 0 deletions template/ios/HelloWorld/RNAssets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}

0 comments on commit 072c071

Please sign in to comment.