iOS脚本化处理*.xcassets文件及图片

asset_manager.sh

功能

将 asset 的图片调用 用方法调用,不再用 UIImage imageNamed

asset 有同名的图片会报错
asset 图片删除 会提醒

#!/bin/bash

获取上一级路径

pro_path=`pwd`

赋值类名和路径

manager=KDAssetImageManager
file_path_h=${pro_path}/ZYBScanSearch/Tools/AssetManager/${manager}.h
file_path_m=${pro_path}/ZYBScanSearch/Tools/AssetManager/${manager}.m

输出声明头文件

echo -e "#import \n\n@interface ${manager}: NSObject \n" > $file_path_h
echo -e "#import \"${manager}.h\" \n\n@implementation ${manager} \n" > $file_path_m

首字母小写

function firster_lowercase () {
    string=$1
    UPPERCASE=`echo ${string:0:1} | tr '[A-Z]' '[a-z]'`
    echo "$UPPERCASE${string:1}"
}

空格(空格隔开的定义为一个文件)

SAVEIFS=$IFS
IFS=$(echo -en "\n\b")


list=`find $pro_path -name '*.imageset'`

for file in $list; do

    resource=$(dirname $file)
    resource=${resource##*/}

    file_name=$(basename $file .imageset)

    method=`echo $file_name | tr -d '[ \t]'`
    method=${method/-/_}
    method=`firster_lowercase ${method}`
    property="/** in ${resource} */\n@property (nonatomic,readonly,class) UIImage *${method};\n"

#属性写入.h文件
    echo -e $property >> $file_path_h
#属性实现写入.m文件
    method="+ (UIImage *)${method} {\n\treturn [UIImage imageNamed:@\"${file_name}\"];\n}\n"
    echo -e $method >> $file_path_m

done

系统常量赋值回去

IFS=$SAVEIFS

echo -e "@end" >> $file_path_h
echo -e "@end" >> $file_path_m

你可能感兴趣的:(iOS脚本化处理*.xcassets文件及图片)