Python一鍵查找iOS項(xiàng)目中未使用的圖片、音頻、視頻資源
前言
在iOS項(xiàng)目開(kāi)發(fā)的過(guò)程中,如果版本迭代開(kāi)發(fā)的時(shí)間比較長(zhǎng),那么在很多版本開(kāi)發(fā)以后或者說(shuō)有多人開(kāi)發(fā)參與以后,工程中難免有一些垃圾資源,未被使用卻占據(jù)著api包的大??!
這里我通過(guò)Python腳本來(lái)查找項(xiàng)目中未被使用的圖片、音頻、視頻資源,然后刪除掉;以達(dá)到減小APP包大小的目的!
代碼
先查找項(xiàng)目中所以的資源文件存到你數(shù)組里面
def searchAllResName(file_dir): global _resNameMap 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 isResource(tmp_path) == True and '/Pods/' not in tmp_path and '.appiconset' not in tmp_path and '.launchimage' not in tmp_path: imageName = tmp_path.split('/')[-1].split('.')[0] _resNameMap[imageName] = tmp_path conLog.info_delRes('[FindRes OK] ' + tmp_path) elif os.path.isdir(tmp_path) and tmp_path.endswith('.imageset') and '/Pods/' not in tmp_path: imageName = tmp_path.split('/')[-1].split('.')[0] _resNameMap[imageName] = tmp_path conLog.info_delRes('[FindRes OK] ' + tmp_path) else: searchAllResName(tmp_path)
遍歷查詢(xún)項(xiàng)目的所以代碼,查找工程中所引用的資源文件
# 查詢(xún)項(xiàng)目的所以代碼 def searchProjectCode(file_dir): global _projectPbxprojPath fs = os.listdir(file_dir) for dir in fs: tmp_path = os.path.join(file_dir, dir) if tmp_path.endswith('project.pbxproj'): _projectPbxprojPath = tmp_path if not os.path.isdir(tmp_path): if '/Pods/' not in tmp_path: try: findResNameAtFileLine(tmp_path) conLog.info_delRes('[ReadFileForRes OK] ' + tmp_path) except Exception as e: pass # conLog.error_delRes('[ReadFileForRes Fail] [' + str(e) + ']' + tmp_path) else: searchProjectCode(tmp_path) # 查找工程中所引用的資源文件 def findResNameAtFileLine(tmp_path): global _resNameMap Ropen = open(tmp_path,'r') for line in Ropen: lineList = line.split('"') for item in lineList: # bar@2x barimg.png if item in _resNameMap or item.split('.')[0] in _resNameMap or item + '@1x' in _resNameMap or item + '@2x' in _resNameMap or item + '@3x' in _resNameMap: del _resNameMap[item] Ropen.close()
刪除垃圾資源文件,這里垃圾資源文件刪除分為兩部分一部分是Assets.xcassets
里面的,一部分是直接導(dǎo)入工程目錄中的資源,如果是Assets.xcassets
垃圾資源直接刪除就行了,但是如果是直接導(dǎo)入到工程目錄里面的資源,那就先刪除project.pbxproj
中的引用,再刪除本地資源文件;
# 刪除無(wú)用的資源文件 def delAllRubRes(): global _resNameMap, _hadDelMap # .imageset類(lèi)型的資源圖片直接刪除 for resName in list(_resNameMap.keys()): tmp_path = _resNameMap[resName] if tmp_path.endswith('.imageset'): if os.path.exists(tmp_path) and os.path.isdir(tmp_path): try: # 已刪除的元素 _hadDelMap[resName] = tmp_path # 刪除.imageset文件夾 delImagesetFolder(tmp_path) # 字典移除 del _resNameMap[resName] conLog.info_delRes('[DelRubRes OK] ' + tmp_path) except Exception as e: conLog.error_delRes('[DelRubRes Fail] [' + str(e) + ']' + tmp_path) else: conLog.error_delRes('[DelRubRes Fail] [not exists] ' + tmp_path) delResAtProjectPbxproj() def delImagesetFolder(rootdir): filelist = [] filelist = os.listdir(rootdir) for f in filelist: filepath = os.path.join( rootdir, f ) if os.path.isfile(filepath): os.remove(filepath) elif os.path.isdir(filepath): shutil.rmtree(filepath,True) shutil.rmtree(rootdir,True) # 直接導(dǎo)入到工程中的圖片需要?jiǎng)h除project.pbxproj中的引用,再移除本地文件 def delResAtProjectPbxproj(): global _projectPbxprojPath, _resNameMap, _hadDelMap if _projectPbxprojPath != None: # 先把需要?jiǎng)h除的資源名先保存一份 _needDelResName = [] file_data = '' Ropen = open(_projectPbxprojPath,'r') for line in Ropen: idAdd = True for resName in _resNameMap: if resName in line: idAdd = False if resName not in _needDelResName: _needDelResName.append(resName) if idAdd == True: file_data += line Ropen.close() Wopen = open(_projectPbxprojPath,'w') Wopen.write(file_data) Wopen.close() # 已經(jīng)清理過(guò)project.pbxproj中的引用的資源文件,開(kāi)始從_resNameMap中移除已被處理過(guò)的資源文件 # 并刪除本地的對(duì)應(yīng)的資源文件 for item in _needDelResName: tmp_path = _resNameMap[item] if os.path.exists(tmp_path) and not os.path.isdir(tmp_path): # 已刪除的元素 _hadDelMap[item] = tmp_path # 刪除文件 os.remove(tmp_path) # 字典移除 del _resNameMap[item] conLog.info_delRes('[DelRubRes OK] ' + tmp_path) else: pass
總的調(diào)用函數(shù)
# 開(kāi)始清理無(wú)用的垃圾資源文件 def startCleanRubRes(file_dir, ignoreList = []): global _resNameMap, _hadDelMap,_isCleaing if _isCleaing == True: return _isCleaing = True initData() conLog.info('-' * 30 + '開(kāi)始清理資源文件' + '-' * 30) searchAllResName(file_dir) conLog.info_delRes('-' * 20 + '全部的資源文件列表' + '-' * 20) conLog.info_delRes(_resNameMap) for item in ignoreList: if item in list(_resNameMap.keys()): del _resNameMap[item] conLog.info_delRes('-' * 20 + '忽略刪除的資源文件' + '-' * 20) conLog.info_delRes(ignoreList) searchProjectCode(file_dir) conLog.info_delRes('-' * 20 + '需要?jiǎng)h除的資源文件' + '-' * 20) conLog.info_delRes(_resNameMap) delAllRubRes() conLog.info_delRes('-' * 20 + '刪除成功的資源文件' + '-' * 20) conLog.info_delRes(_hadDelMap) conLog.info_delRes('-' * 20 + '刪除失敗的資源文件' + '-' * 20) conLog.info_delRes(_resNameMap) _isCleaing = False
軟件
鑒于有些iOS開(kāi)發(fā)程序員沒(méi)有Python基礎(chǔ),這里做了一個(gè)圖形化操作界面,歡迎大家下載使用!
下載地址:
https://gitee.com/zfj1128/ZFJ...
軟件截圖:
總結(jié)
以上所述是小編給大家介紹的Python一鍵查找iOS項(xiàng)目中未使用的圖片、音頻、視頻資,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
欄 目:IOS
下一篇:iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果
本文標(biāo)題:Python一鍵查找iOS項(xiàng)目中未使用的圖片、音頻、視頻資源
本文地址:http://mengdiqiu.com.cn/a1/IOS/11881.html
您可能感興趣的文章
- 01-11iOS查找私有API的方法示例


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-11UILabel顯示定時(shí)器文本跳動(dòng)問(wèn)題的解決
- 01-11iOS常用算法之兩個(gè)有序數(shù)組合并(要
- 01-11iOS 彈幕功能的實(shí)現(xiàn)思路圖解
- 01-11詳解MacOs免密登錄CentOs操作步驟
- 01-11iOS動(dòng)態(tài)更換Icon的全過(guò)程記錄
- 01-11iOS調(diào)試Block引用對(duì)象無(wú)法被釋放的小技
- 01-11iOS常見(jiàn)宏理解及使用方法
- 01-11iOS實(shí)現(xiàn)文本分頁(yè)的方法示例
- 01-11iOs遷至WKWebView跨過(guò)的一些坑
- 01-11iOS模擬中獎(jiǎng)名單循環(huán)滾動(dòng)效果
隨機(jī)閱讀
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery