前言
花了一個版本專門優化公司APP包的大小,鑑於設計我來之前設計給的圖片都是未壓縮的,我先從圖片方面進行優化的;以前我也專門寫過關於ipa瘦身的文章:
👉《iOS-APP包的瘦身之旅(從116M到現在的36M的減肥之路)》👈
我們知道針對ipa的瘦身,資源是佔大頭的!
TinyPNG
這裏我使用的是TinyPNG《https://tinypng.com》,TinyPNG也提供了Python的庫,大家使用的時候註冊賬號申請Key就行了,需要說明的是TinyPNG免費一個月只能壓縮500張,如果針對小的項目來說500張已經夠了,要是公司的項目比較大,可以選擇多註冊幾個賬號😂,我是建議要是有能力還是購買一下,畢竟別人維護更新也不易,要是資金比較緊張或者公司是個鐵公雞就算了,就勉爲其難的薅薅羊毛吧!😂
詳細可以看TinyPNG的Python的API!
主要代碼如下:
import tinify
tinify.key = "sDz081wW56kWgFncVqD1nm5vxykbJjgt" # 你的祕鑰
source = tinify.from_file("right_btn.jpg") # 壓縮前
source.to_file("right_btn.jpg") # 壓縮後
源碼
代碼方面我準備了多個Key,然後記錄壓縮的日誌,話不多說上代碼,部分主要如下:
import tinify
import os
import sys
import ASHelp
import ASCutDownView
from ASPersoninfo import *
tinify_key_list = ['sDz081wW56kWgFncVqD1nm5vxykbJjgt', # 已經超過500
'dX8rL9C6bpsjcx0NjFdw6rSWTWg8JwbR', # 已經超過500
]
# tinify.key = 'TTYlnmj9xX1vhvdpfDFZwMTxrsj13Kkt'
# 所有的圖片路徑
_all_png_path_list = []
# 已經壓縮的
_already_compressed_list = []
# 項目總路徑
_file_dir = None
# 單利
_personinfo = ASPersoninfo()
# 獲取路徑
def get_path(fileName):
path = os.path.join(os.path.dirname(sys.argv[0]), fileName)
return path
# 已經壓縮的圖片路徑日誌
_already_compressed_path = get_path('already_compressed.txt')
# 判斷是不是圖片
def is_image(tmp_path):
imageType = imghdr.what(tmp_path)
return imageType
# 讀取所以的png圖片
def read_all_png_image(file_dir):
global _all_png_path_list, _personinfo
fs = os.listdir(file_dir)
for dir in fs:
tmp_path = os.path.join(file_dir, dir)
if not os.path.isdir(tmp_path):
if is_ignore_path(tmp_path) == False and tmp_path.endswith('.png'):
# 當前圖片即不在需要壓縮的圖片中 又不在已經壓縮過的圖片裏面
if tmp_path not in _all_png_path_list and tmp_path not in _already_compressed_list:
_all_png_path_list.append(tmp_path)
else:
if _personinfo.is_stop == True:
# 停止了
pass
else:
read_all_png_image(tmp_path)
# 保存已經壓縮的圖片路徑
def save_already_compressed_path_list_txt():
global _already_compressed_list
create_already_compressed_txt()
file_data = ''
# 保存 _already_compressed_list
for item in _already_compressed_list:
file_data += item + '\n'
ropen = open(_already_compressed_path, 'w', encoding='utf-8')
ropen.write(file_data)
ropen.close()
# 讀取已經壓縮的文件
def read_already_compressed_path_list_txt():
create_already_compressed_txt()
# 讀取
_history_text_list = []
ropen = open(_already_compressed_path,'r')
for line in ropen:
line = line.replace('\n', '')
if line.endswith('.png'):
_already_compressed_list.append(line)
# 簡化日誌輸出
new_line_list = line.split('/')
if len(new_line_list) >= 3:
new_line_list = new_line_list[len(new_line_list) - 3:]
new_line = '/'.join(new_line_list)
new_line = '.../' + new_line
_history_text_list.append(new_line)
return '\n'.join(_history_text_list)
# 創建already_compressed.txt
def create_already_compressed_txt():
if os.path.exists(_already_compressed_path) == False:
# 不存在
Wopen = open(_already_compressed_path, 'w')
Wopen.close()
else:
# 已存在
pass
# 是不是忽略的路徑
def is_ignore_path(tmp_path):
# 先判斷是不是忽略的圖片
global _personinfo
if len(_personinfo.ignore_img_list) > 0:
for image_name in _personinfo.ignore_img_list:
if '/' + image_name + '.' in tmp_path:
return True
elif '/' + image_name + '@' in tmp_path:
return True
if '/.' in tmp_path:
return True
elif '/Pods' in tmp_path:
return True
elif '.bundle/' in tmp_path:
return True
else:
return False
# 開始壓縮圖片
def start_compress_png_image(tmp_path):
global _all_png_path_list
if tmp_path not in _all_png_path_list:
_all_png_path_list.append(tmp_path)
#這裏就是通過tingPng壓縮圖片的核心代碼
def compress_core(file, outputFile):
# 已經使用過多少次
compressions_this_month = str(tinify.compression_count)
source = tinify.from_file(file) #壓縮指定文件
source.to_file(outputFile) #將壓縮後的文件輸出當指定位置
# 壓縮所有的項目圖片
def compress_all_png_image():
global _all_png_path_list, _already_compressed_list, _personinfo
for png_path in _all_png_path_list:
if _personinfo.is_stop == True:
# 停止了
break
# 當前圖片沒有壓縮
if png_path not in _already_compressed_list:
try:
source = tinify.from_file(png_path)
source.to_file(png_path)
# 壓縮過了 添加到壓縮圖片中
_already_compressed_list.append(png_path)
# 日誌
ASHelp.info('壓縮成功:' + png_path)
except Exception as error:
ASHelp.error('壓縮失敗:' + png_path + '【' + str(error) +'】')
break
# 驗證卡密的
def tinify_validate(key):
try:
tinify.key = key
tinify.validate()
return None
except Exception as error:
return str(error)
# 停止壓縮了 0系統停止的 1手動停止的
def did_stop_compress():
if _personinfo.is_stop == False:
ASHelp.info('-' * 20 + '圖片壓縮結束' + '-' * 20)
else:
ASHelp.info('-' * 20 + '手動停止壓縮' + '-' * 20)
ASCutDownView.stop_tiny_image()
# 清空歷史記錄
def clean_history_txt():
global _already_compressed_path
create_already_compressed_txt()
try:
os.remove(_already_compressed_path)
return None
except Exception as error:
return error
# 初始化
def init_data():
global _all_png_path_list, _already_compressed_list, _file_dir, _personinfo
_all_png_path_list = []
_already_compressed_list = []
_file_dir = None
# 設置卡密
tinify.key = _personinfo.tinify_key
# 重新開始了
_personinfo.is_stop = False
# 開始壓縮項目的圖片
def start_compress_pro_image(file_dir):
global _file_dir, _personinfo
init_data()
if len(_personinfo.tinify_key) == 0:
# 錯誤拋出
ASHelp.error('-' * 20 + '沒有設置卡密' + '-' * 20)
return
ASHelp.info('-' * 20 + '圖片壓縮開始' + '-' * 20)
_file_dir = file_dir
# 先讀取已經壓縮的圖片
read_already_compressed_path_list_txt()
# 開始讀取項目中的圖片
read_all_png_image(_file_dir)
# 讀取了所有的圖片,開始壓縮所有的圖片
compress_all_png_image()
# 保存已經壓縮的圖片
save_already_compressed_path_list_txt()
# 壓縮結束
did_stop_compress()
if __name__ == '__main__':
file_dir = '/Users/administrator/Desktop/ahaschool_ios/Ahaschool'
start_compress_pro_image(file_dir)
結束語
因爲有些iOS開發者並不會Python,我後來把腳本改爲GUI的桌面程序了,小白簡單易操作,界面如下:
改軟件有時間我會把它放到GIT上的,也可以私聊我獲取!
話說…😁😁😁
既然已經看完了,各位大佬點個贊才走唄!😍