先贴脚本地址autoExportAppIcon
1、介绍
作为iOS开发者,或多或少要接触到app图标切图,对于公司没有美工或者美工对iOS图标尺寸不了解的iOS开发者来说,每次制作图标要么自己切图半天,要么跟美工解释半天,心也是很累。索性写了个脚本直接生成图标,一分钟换图标,不要太爽!
2、原理及用法
脚本使用python
写的,用的库是有名的PIL
库。让美工提供一张不小于1024x1024
的方图,针对不同尺寸图标设置resize
图片,并修改Contents.json
(app图标信息文件,xcode中需要)。
用法:下载脚本,并在终端下执行python /path/autoExportAppIcon.py /path/image.jpg
在xcode中新建
AppIcon
:
打开
AppIcon
文件夹:
将生成的图标及
Content.json
文件替换就行了。
3、贴代码
#!/usr/bin/env python
# coding: utf-8
import sys
import os
try:
from PIL import Image
except:
print ('\033[31m' + '缺少Image模块,正在安装Image模块,请等待...' + '\033[0m')
success = os.system('python -m pip install Image')
if success == 0:
print('\033[7;32m' + 'Image模块安装成功.' + '\033[0m')
from PIL import Image
else:
print ('\033[31m' + 'Image安装失败,请手动在终端执行:\'python -m pip install Image\'重新安装.' + '\033[0m')
quit()
outPutPath = os.path.expanduser('~') + '/Desktop/AppIcon/'
if not os.path.exists(outPutPath):
os.mkdir(outPutPath)
if len(sys.argv) <= 1:
print ('\033[31m' + '请输入图片路径,eg: python autoExportAppIcon.py /path/xxx.png' + '\033[0m')
quit()
ImageName = sys.argv[1]
# print('图片名字为:' + ImageName)
originImg = ''
try:
originImg = Image.open(ImageName)
except:
print ('\033[31m' + '\'' + ImageName + '\'' + ',该文件不是图片文件,请检查文件路径.' + '\033[0m')
quit()
# 20x20
img0 = originImg.resize((20,20), Image.ANTIALIAS)
img1 = originImg.resize((40,40), Image.ANTIALIAS)
img2 = originImg.resize((60,60), Image.ANTIALIAS)
img0.save(outPutPath + 'appIcon20x20.png',"png")
img1.save(outPutPath + 'appIcon20x20@2x.png',"png")
img2.save(outPutPath + 'appIcon20x20@3x.png',"png")
# 29x29
img3 = originImg.resize((29,29), Image.ANTIALIAS)
img4 = originImg.resize((58,58), Image.ANTIALIAS)
img5 = originImg.resize((87,87), Image.ANTIALIAS)
img3.save(outPutPath + 'appIcon29x29.png',"png")
img4.save(outPutPath + 'appIcon29x29@2x.png',"png")
img5.save(outPutPath + 'appIcon29x29@3x.png',"png")
# 40x40
img6 = originImg.resize((40,40), Image.ANTIALIAS)
img7 = originImg.resize((80,80), Image.ANTIALIAS)
img8 = originImg.resize((120,120), Image.ANTIALIAS)
img6.save(outPutPath + 'appIcon40x40.png',"png")
img7.save(outPutPath + 'appIcon40x40@2x.png',"png")
img8.save(outPutPath + 'appIcon40x40@3x.png',"png")
# 60x60
img9 = originImg.resize((120,120), Image.ANTIALIAS)
img10 = originImg.resize((180,180), Image.ANTIALIAS)
img9.save(outPutPath + 'appIcon60x60@2x.png',"png")
img10.save(outPutPath + 'appIcon60x60@3x.png',"png")
# ipad
img11 = originImg.resize((76,76), Image.ANTIALIAS)
img12 = originImg.resize((152,152), Image.ANTIALIAS)
img13 = originImg.resize((167,167), Image.ANTIALIAS)
img11.save(outPutPath + 'appIcon76x76.png',"png")
img12.save(outPutPath + 'appIcon76x76@2x.png',"png")
img13.save(outPutPath + 'appIcon83.5x83.5@2x.png',"png")
# 创建Contents.json文件
content = '''
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"filename" : "appIcon20x20@2x.png",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"filename" : "appIcon20x20@3x.png",
"scale" : "3x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "appIcon29x29.png",
"scale" : "1x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "appIcon29x29@2x.png",
"scale" : "2x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "appIcon29x29@3x.png",
"scale" : "3x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "appIcon40x40@2x.png",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "appIcon40x40@3x.png",
"scale" : "3x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "appIcon60x60@2x.png",
"scale" : "2x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "appIcon60x60@3x.png",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"filename" : "appIcon20x20.png",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"filename" : "appIcon20x20@2x.png",
"scale" : "2x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "appIcon29x29.png",
"scale" : "1x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "appIcon29x29@2x.png",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "appIcon40x40.png",
"scale" : "1x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "appIcon40x40@2x.png",
"scale" : "2x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "appIcon76x76.png",
"scale" : "1x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "appIcon76x76@2x.png",
"scale" : "2x"
},
{
"size" : "83.5x83.5",
"idiom" : "ipad",
"filename" : "appIcon83.5x83.5@2x.png",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
'''
f = open(outPutPath + 'Contents.json', 'w')
f.write(content)
print('\033[7;32m' + '文件输出文件夹:' + outPutPath + '\033[0m')
os.system('open ' + outPutPath)
注:xcode
版本为8.0
如使用有问题,请留言.