Skip to content

Instantly share code, notes, and snippets.

@chrissearle
Created October 20, 2021 20:52
Show Gist options
  • Save chrissearle/9e96103b47b6fb98a5fed6aff6d6afb8 to your computer and use it in GitHub Desktop.
Save chrissearle/9e96103b47b6fb98a5fed6aff6d6afb8 to your computer and use it in GitHub Desktop.
Create xcassets from three directories of PNG images - 1x, 2x and 3x
#!/bin/zsh
# Assumes you start with three directories of images in png format with the same name - 1x, 2x and 3x
# Call with four params
# zip_directories_to_xcassets.zsh name_of_asset_file directory_of_1x_pngs directory_of_2x_pngs directory_of_3x_pngs
ASSETS_DIR=$1.xcassets
D1=$2
D2=$3
D3=$4
mkdir -p $ASSETS_DIR
cat <<EOT > $ASSETS_DIR/Contents.json
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
EOT
for F in `ls $D1`; do
ROOT_NAME=${F%%.png}
mkdir -p $ASSETS_DIR/$ROOT_NAME.imageset
cat <<EOT > $ASSETS_DIR/$ROOT_NAME.imageset/Contents.json
{
"images" : [
{
"filename" : "$ROOT_NAME.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "$ROOT_NAME-2.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "$ROOT_NAME-3.png",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
EOT
cp $D1/$ROOT_NAME.png $ASSETS_DIR/$ROOT_NAME.imageset/$ROOT_NAME.png
cp $D2/$ROOT_NAME.png $ASSETS_DIR/$ROOT_NAME.imageset/$ROOT_NAME-2.png
cp $D3/$ROOT_NAME.png $ASSETS_DIR/$ROOT_NAME.imageset/$ROOT_NAME-3.png
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment