简体   繁体   English

如何从 Xcode 项目中删除未使用的图像

[英]How to remove unused images from an Xcode project

I'm following this link to find the unused images from an xcode project but they are not working well for me.我正在点击此链接从 xcode 项目中查找未使用的图像,但它们对我来说效果不佳。 I tried Rob's answer and Roman's answer but they give me all the png images.我尝试了 Rob 的回答和 Roman 的回答,但他们给了我所有的 png 图像。 I tried to use the tool it also giving me all png images我尝试使用该工具,它还为我提供了所有 png 图像

EDIT编辑

The way Im using script我使用脚本的方式
1)i copy script and paste it in the terminal and press Enter. 1)我复制脚本并将其粘贴到终端中,然后按 Enter 键。

2) save the script in txt file and in terminal i type sh myscript.txt 2) 将脚本保存在 txt 文件中,然后在终端中输入sh myscript.txt

在此处输入图像描述

I'm following this link to find the unused images from an xcode project but they are not working well for me.我正在按照此链接从 xcode 项目中查找未使用的图像,但它们对我来说效果不佳。 I tried Rob's answer and Roman's answer but they give me all the png images.我尝试了 Rob 的回答和 Roman 的回答,但他们给了我所有的 png 图像。 I tried to use the tool it also giving me all png images我尝试使用该工具,它还为我提供了所有 png 图像

EDIT编辑

The way Im using script我使用脚本的方式
1)i copy script and paste it in the terminal and press Enter. 1)我复制脚本并将其粘贴到终端中,然后按 Enter。

2) save the script in txt file and in terminal i type sh myscript.txt 2) 将脚本保存在 txt 文件中,然后在终端中输入sh myscript.txt

在此处输入图片说明

I have created a python script to identify the unused images in a swift project: 'unused_assets.py' @ gist , which can be easily tweaked for an objective-c project as yours.我已经创建了一个 python 脚本来识别swift项目中未使用的图像: 'unused_assets.py'@gist ,它可以很容易地针对您的 objective-c 项目进行调整。

The script can be used like this:该脚本可以这样使用:

python3 unused_assets.py '/Users/DevK/MyProject' '/Users/DevK/MyProject/MyProject/Assets/Assets.xcassets'

Here are few rules to use the script:以下是使用该脚本的一些规则:

  • It is important to pass project folder path as first argument, assets folder path as second argument将项目文件夹路径作为第一个参数传递,将资产文件夹路径作为第二个参数传递很重要
  • It is assumed that all the images are maintained within Assets.xcassets folder and are used either within swift/ objective-c files or within storyboards假定所有图像都保存在 Assets.xcassets 文件夹中,并在 swift/objective-c 文件或故事板中使用

Limitations in first version:第一版的限制:

  • Doesn't work for objective c files, but can be tweaked to achieve expected behavior as specified in comment in below code不适用于目标 c 文件,但可以进行调整以实现下面代码中注释中指定的预期行为

I will try to improve it over the time, based on feedback, however the first version should be good for most.随着时间的推移,我会根据反馈尝试改进它,但是第一个版本应该适合大多数人。

Here is the code.这是代码。 I tried to make the code self explanatory by adding appropriate comments to each important step, let me know if you still need more details .我试图通过为每个重要步骤添加适当的注释来使代码不言自明,如果您还需要更多详细信息,请告诉我

# Usage e.g.: python3 unused_assets.py '/Users/DevK/MyProject' '/Users/DevK/MyProject/MyProject/Assets/Assets.xcassets'
# It is important to pass project folder path as first argument, assets folder path as second argument
# It is assumed that all the images are maintained within Assets.xcassets folder and are used either within swift files or within storyboards

"""
@author = "Devarshi Kulshreshtha"
@copyright = "Copyright 2020, Devarshi Kulshreshtha"
@license = "GPL"
@version = "1.0.1"
@contact = "kulshreshtha.devarshi@gmail.com"
"""

import sys
import glob
from pathlib import Path
import mmap
import os
import time

# obtain start time
start = time.time()

arguments = sys.argv

# pass project folder path as argument 1
projectFolderPath = arguments[1].replace("\\", "") # replacing backslash with space
# pass assets folder path as argument 2
assetsPath = arguments[2].replace("\\", "") # replacing backslash with space

print(f"assetsPath: {assetsPath}")
print(f"projectFolderPath: {projectFolderPath}")

# obtain all assets / images 
# obtain paths for all assets

assetsSearchablePath = assetsPath + '/**/*.imageset'  #alternate way to append: fr"{assetsPath}/**/*.imageset"
print(f"assetsSearchablePath: {assetsSearchablePath}")

imagesNameCountDict = {} # empty dict to store image name as key and occurrence count
for imagesetPath in glob.glob(assetsSearchablePath, recursive=True):
    # storing the image name as encoded so that we save some time later during string search in file 
    encodedImageName = str.encode(Path(imagesetPath).stem)
    # initializing occurrence count as 0
    imagesNameCountDict[encodedImageName] = 0

print("Names of all assets obtained")

# search images in swift files
# obtain paths for all swift files

# For objective-c project, change '/**/*.swift' to '/**/*.m' 
swiftFilesSearchablePath = projectFolderPath + '/**/*.swift' #alternate way to append: fr"{projectFolderPath}/**/*.swift"
print(f"swiftFilesSearchablePath: {swiftFilesSearchablePath}")

for swiftFilePath in glob.glob(swiftFilesSearchablePath, recursive=True):
    with open(swiftFilePath, 'rb', 0) as file, \
        mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
        # search all the assests within the swift file
        for encodedImageName in imagesNameCountDict:
            # file search
            if s.find(encodedImageName) != -1:
                # updating occurrence count, if found 
                imagesNameCountDict[encodedImageName] += 1

print("Images searched in all swift files!")

# search images in storyboards
# obtain path for all storyboards

storyboardsSearchablePath = projectFolderPath + '/**/*.storyboard' #alternate way to append: fr"{projectFolderPath}/**/*.storyboard"
print(f"storyboardsSearchablePath: {storyboardsSearchablePath}")
for storyboardPath in glob.glob(storyboardsSearchablePath, recursive=True):
    with open(storyboardPath, 'rb', 0) as file, \
        mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
        # search all the assests within the storyboard file
        for encodedImageName in imagesNameCountDict:
            # file search
            if s.find(encodedImageName) != -1:
                # updating occurrence count, if found
                imagesNameCountDict[encodedImageName] += 1

print("Images searched in all storyboard files!")
print("Here is the list of unused assets:")

# printing all image names, for which occurrence count is 0
print('\n'.join({encodedImageName.decode("utf-8", "strict") for encodedImageName, occurrenceCount in imagesNameCountDict.items() if occurrenceCount == 0}))

print(f"Done in {time.time() - start} seconds!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM