当前位置: 首页 > 后端技术 > Python

Python-一键查找iOS项目中未使用的图片、音视频资源

时间:2023-03-26 12:12:47 Python

前言在iOS项目开发过程中,如果版本的迭代开发时间比较长,那么在开发了很多版本之后或者多人参与开发之后,项目中难免会出现一些垃圾资源,未使用但占用API包。尺寸!这里我使用Python脚本找到项目中不用的图片、音视频资源,然后删除;以达到减小APP包大小的目的!该代码首先搜索项目中的所有资源文件并将它们保存在您的数组defsearchAllResName(file_dir):global_resNameMapfs=os.listdir(file_dir)fordirinfs:tmp_path=os.path.join(file_dir,dir)ifnotos.path.isdir(tmp_path):ifisResource(tmp_path)==True和'/Pods/'不在tmp_path和'.appiconset'不在tmp_path和'.launchimage'不在tmp_path:imageName=tmp_path.split('/')[-1].split('.')[0]_resNameMap[imageName]=tmp_pathconLog.info_delRes('[FindResOK]'+tmp_path)elifos.path.isdir(tmp_path)和tmp_path。endswith('.imageset')和'/Pods/'不在tmp_path中:imageName=tmp_path.split('/')[-1].split('.')[0]_resNameMap[imageName]=tmp_pathconLog.info_delRes('[FindResOK]'+tmp_path)else:searchAllResName(tmp_path)遍历查询项目的所有代码,查找项目中引用的资源文件#查询项目的所有代码defsearchProjectCode(file_dir):global_projectPbxprojPathfs=os.listdir(file_dir)fordirinfs:tmp_path=操作系统。path.join(file_dir,dir)iftmp_path.endswith('project.pbxproj'):_projectPbxprojPath=tmp_pathifnotos.path.isdir(tmp_path):if'/Pods/'notintmp_path:尝试:findResNameAtFileLine(tmp_path)conLog.info_delRes('[ReadFileForResOK]'+tmp_path)除了Exceptionase:pass#conLog.error_delRes('[ReadFileForResFail]['+str(e)+']'+tmp_path)else:searchProjectCode(tmp_path)#找到项目中引用的资源文件deffindResNameAtFileLine(tmp_path):global_resNameMapRopen=open(tmp_path,'r')forlineinRopen:lineList=line.split('"')foriteminlineList:#bar@2x巴林格.png如果itemin_resNameMap或item.split('.')[0]in_resNameMap或item+'@1x'in_resNameMap或item+'@2x'in_resNameMap或item+'@3x'in_resNameMap:del_resNameMap[item]Ropen.close()删除垃圾资源文件。这里的垃圾资源文件删除分为两部分。一个在Assets.xcassets中,一个直接导入到项目目录下的资源中。如果是Assets.xcassets垃圾资源,直接删除即可。但如果是直接导入到项目目录下的资源,先删除project.pbxproj中的引用,再删除本地资源文件;#删除无用的资源文件defdelAllRubRes():global_resNameMap,_hadDelMap#.imagesettyperesource直接删除图片forresNameinlist(_resNameMap.keys()):tmp_path=_resNameMap[resName]iftmp_path.endswith('.imageset'):ifos.path.exists(tmp_path)andos.path.isdir(tmp_path):try:#Deletedelement_hadDelMap[resName]=tmp_path#Delete.imagesetfolderdelImagesetFolder(tmp_path)#Dictionaryremovaldel_resNameMap[resName]conLog.info_delRes('[DelRubResOK]'+tmp_path)除了异常为e:conLog.error_delRes('[DelRubResFail]['+str(e)+']'+tmp_path)else:conLog.error_delRes('[DelRubResFail][notexists]'+tmp_path)delResAtProjectPbxproj()defdelImagesetFolder(rootdir):filelist=[]filelist=os.listdir(rootdir)forfinfilelist:filepath=os.path.join(rootdir,f)ifos.path.isfile(filepath):os.remove(filepath)elifos.path.isdir(filepath):shutil.rmtree(filepath,True)shutil.rmtree(rootdir,True)#对于直接导入到项目中的图片,需要删除project.pbxproj中的引用,并且thenthelocalFiledefdelResAtProjectPbxproj():global_projectPbxprojPath,_resNameMap,_hadDelMapif_projectPbxprojPath!=None:#先保存一份需要删除的资源名。_needDelResName=[]file_data=''Ropen=open(_projectPbxprojPath,'r')forlineinRopen:idAdd=TrueforresNamein_resNameMap:如果resName在行中:idAdd=False如果resName不在_needDelResName中:_needDelResName.append(resName)如果idAdd==True:file_data+=lineRopen.close()Wopen=open(_projectPbxprojPath,'w')Wopen.write(file_data)Wopen.close()#project.pbxproj中引用的资源文件已经清理,处理过的资源文件从_resNameMap中移除#并删除itemin_needDelResName对应的本地资源文件:tmp_path=_resNameMap[item]ifos.path.exists(tmp_path)andnotos.path.isdir(tmp_path):#删除元素_hadDelMap[item]=tmp_path#删除文件os.remove(tmp_path)#dictionaryshiftExceptdel_resNameMap[item]conLog.info_delRes('[DelRubResOK]'+tmp_path)else:pass总调用函数#开始清理无用的垃圾资源文件defstartCleanRubRes(file_dir,ignoreList=[]):global_resNameMap,_hadDelMap,_isCleaingif_isCleaing==True:return_isCleaing=TrueinitData()conLog.info('-'*30+'开始清理资源文件'+'-'*30)searchAllResName(file_dir)conLog.info_delRes('-'*20+'所有资源文件列表'+'-'*20)conLog.info_delRes(_resNameMap)foriteminignoreList:ifiteminlist(_resNameMap.keys()):del_resNameMap[item]conLog。info_delRes('-'*20+'忽略删除的资源文件'+'-'*20)conLog.info_delRes(ignoreList)searchProjectCode(file_dir)conLog.info_delRes('-'*20+'要删除的资源文件'+'-'*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=FalseSoftware针对一些没有Python基础的iOS开发程序员,这里提供图形化操作界面,欢迎大家下载使用!下载地址:https://gitee.com/zfj1128/ZFJ...软件截图: