iOS 项目中 1 倍图检索

在项目开发的过程中,图片资源有些 1 倍图还存在,但是目前 iOS 开发的版本都支持 iOS 6 以上,甚至大部分只支持到 iOS 8 或者 iOS 9 , 所以这些 1 倍图资源可以直接删除了,可以通过下面的脚本大概检索一下项目中 1 倍图的数量,预估一下工作量

image delete py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
# coding=utf-8
#!/usr/bin/env python

import os
import fnmatch
import argparse

__author__ = 'huidragonaijy@gmail.com'
__version__ = '0.0.1'
__copyright__ = 'MIT License'

BLUE = '\033[1;94m'
GREEN = '\033[1;92m'
ENDC = '\033[0m'


def iter_files(path, fnexp):
for root, dirs, files in os.walk(path):
for filename in fnmatch.filter(files, fnexp):
yield os.path.join(root, filename)


def extant_dir(x):
if not os.path.exists(x):
# Argparse uses the ArgumentTypeError to give a rejection message like:
# error: argument input: x does not exist
raise argparse.ArgumentTypeError("{0} does not exist".format(x))
elif not os.path.isdir(x):
raise argparse.ArgumentTypeError("{0} isn't a directory".format(x))
return x


def main(argv=None):
parser = argparse.ArgumentParser(description='find all 1x image in the specified directory.')
parser.add_argument('--version', action='version', version=__version__)
parser.add_argument('path', help='destination directory', type=extant_dir)
args = parser.parse_args(argv)

total = 0
for file_path in iter_files(args.path, '*@2x.png'):
file_name = os.path.basename(file_path)
(name, _) = file_name.split('@')
if not list(iter_files(args.path, name + '@3x.png')):
continue
imgs_1x = list(iter_files(args.path, name + '.png'))
if imgs_1x:
total += 1
print BLUE + '=> ' + name + ENDC
for img in imgs_1x:
print img
print u'\U0001F37B ' + GREEN + 'total: %d' % total + ENDC


if __name__ == '__main__':
main()

删除注意

  1. 删除图片的时候要查找每一张图片使用的情况
  2. imageName: 加载图片的时候,最好不要使用 .png 后缀,否则只加载了 1 倍图,那么如果将 1 倍图删除了,就会出现问题
-------------本文结束谢谢欣赏-------------
Alice wechat