iOS删除项目中未使用的图片资源
随着项目版本不断的迭代开发,项目中的图片资源文件也不断的替换,如果没有养成一个好的使用习惯,时间久了,就会容易产生很多无用图片资源,如果手动去查找删除那么效率会非常低,于是写了一个python脚本来自动查找删除这些没用的图片资源。文本搜索使用的是the silver searcher(ag)
,因为使用grep
搜索代码用户体验实在太差了。在日常操作中,我们也可以用ag
取代grep
。
使用方式很简单,下面来介绍 the silver searcher
的安装方法和python脚本的使用方法。脚本目前只能清除.xcassets
文件中未被使用的图片
一、安装 the silver searcher
这里只介绍MacOS安装方式:
brew install the_silver_searcher
安装成功之后,系统会新增一个ag
指令来使用the silver searcher
,使用方式:ag [FILE-TYPE] [OPTIONS] PATTERN [PATH]
,例如:
mac:Desktop mac$ ag "record_empty" /Users/mac/Desktop/Demo
/Users/mac/Desktop/Demo/Demo/ViewController.swift
15: let image = UIImage(named: "record_empty")
mac:Desktop mac$
二、查找 .xcassets 文件夹脚本
find_xcassets()
函数作用是查找路径root
下的所有.xcassets
文件夹,代码如下:
import os
import re
root_path = "/Users/mac/Desktop/Code/beijingbus_ios/BJBus"
xcasset_paths = []
def find_xcassets(root):
items = os.listdir(root)
for item in items:
path = os.path.join(root, item)
if is_xcassets_dir(item):
xcasset_paths.append(path)
print '[+] %s' % path
elif os.path.isdir(path):
find_xcassets(path)
return xcasset_paths
def is_xcassets_dir(dir):
return re.search(r'.xcassets', dir)
if __name__ == '__main__':
find_xcassets(root_path)
运行脚本会找出出项目中所有的.xcassets
文件,例如
mac:Desktop mac$ python find_xcassets.py
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Login/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/BusLines/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Trip/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Order/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Me/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Message/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/ETABus/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/QRCode/resources/Image.xcassets
mac:Desktop mac$
三、查找 .xcassets 中未使用的图片资源脚本
remove_unused_images.py
文件中引用了find_xcassets.py
文件中的find_xcassets()
函数,代码如下:
import glob
import os
import re
from find_xcassets import *
# 搜索目标路径
path = '/Users/mac/Desktop/Code/beijingbus_ios/BJBus'
# 忽略的图片名称正则数组
ignores = {r'guide_\d+'}
def find_unused_images_at_path(root):
xcassets = find_xcassets(root)
unused_images = []
for xcasset in xcassets:
images = glob.glob(xcasset + '/*/*.imageset')
unused_images += find_unused_images(images)
text_path = 'unused_images.txt'
text = '\n'.join(sorted(unused_images))
os.system('echo "%s" > %s' % (text, text_path))
print 'unused images:%d' % (len(unused_images))
print 'Done!'
def find_unused_images(images):
image_names = [os.path.basename(image)[:-len(".imageset")] for image in images]
unused_images = []
for i in range(0, len(images)):
image_name = image_names[i]
if is_ignore(image_name):
continue
command = 'ag "%s" %s' % (image_name, path)
result = os.popen(command).read()
if result == '':
unused_images.append(images[i])
print 'remove %s' % (images[i])
os.system('rm -rf %s' % (images[i]))
return unused_images
def is_ignore(image_name):
for ignore in ignores:
if re.match(ignore, image_name):
return True
return False
if __name__ == '__main__':
find_unused_images_at_path(path)
四、脚本使用方式:
1、修改remove_unused_images.py
文件中的搜索目标路径path
为你的工程路径;
2、在文件remove_unused_images.py
中设置要忽略的图片,修改数组ignores
,元素可以是正则表达式字符串;
3、在终端运行脚本:
mac:Desktop mac$ python remove_unused_images.py
remove /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/BusLines/resources/Image.xcassets/BusLines/buslines_search.imageset
remove /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/BusLines/resources/Image.xcassets/BusLines/bus_lines_direction.imageset
remove /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/BusLines/resources/Image.xcassets/BusLines/bus_lines_share.imageset
remove /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Order/resources/Image.xcassets/BusOrder/bus_order_price.imageset
remove /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Order/resources/Image.xcassets/BusOrder/bus_order_score.imageset
remove /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Order/resources/Image.xcassets/SubwayOrder/subway_order_time.imageset
unused images:6
Done!
mac:Desktop mac$
4、最后会在当前路径下生成一个日志文件unused_images.txt
,内容为移除的图片路径。
代码地址:https://github.com/wlFlyingFish/scripts/tree/main/fund_unused_images