飞道的博客

Python-批量要是iOS项目中的图片,减小IPA包的大小

595人阅读  评论(0)

前言

花了一个版本专门优化公司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上的,也可以私聊我获取!
话说…😁😁😁
既然已经看完了,各位大佬点个赞才走呗!😍


转载:https://blog.csdn.net/u014220518/article/details/105973249
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场