簡體   English   中英

如何從 Xcode 項目中刪除未使用的圖像

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

我正在點擊此鏈接從 xcode 項目中查找未使用的圖像,但它們對我來說效果不佳。 我嘗試了 Rob 的回答和 Roman 的回答,但他們給了我所有的 png 圖像。 我嘗試使用該工具,它還為我提供了所有 png 圖像

編輯

我使用腳本的方式
1)我復制腳本並將其粘貼到終端中,然后按 Enter 鍵。

2) 將腳本保存在 txt 文件中,然后在終端中輸入sh myscript.txt

在此處輸入圖像描述

我正在按照此鏈接從 xcode 項目中查找未使用的圖像,但它們對我來說效果不佳。 我嘗試了 Rob 的回答和 Roman 的回答,但他們給了我所有的 png 圖像。 我嘗試使用該工具,它還為我提供了所有 png 圖像

編輯

我使用腳本的方式
1)我復制腳本並將其粘貼到終端中,然后按 Enter。

2) 將腳本保存在 txt 文件中,然后在終端中輸入sh myscript.txt

在此處輸入圖片說明

我已經創建了一個 python 腳本來識別swift項目中未使用的圖像: 'unused_assets.py'@gist ,它可以很容易地針對您的 objective-c 項目進行調整。

該腳本可以這樣使用:

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

以下是使用該腳本的一些規則:

  • 將項目文件夾路徑作為第一個參數傳遞,將資產文件夾路徑作為第二個參數傳遞很重要
  • 假定所有圖像都保存在 Assets.xcassets 文件夾中,並在 swift/objective-c 文件或故事板中使用

第一版的限制:

  • 不適用於目標 c 文件,但可以進行調整以實現下面代碼中注釋中指定的預期行為

隨着時間的推移,我會根據反饋嘗試改進它,但是第一個版本應該適合大多數人。

這是代碼。 我試圖通過為每個重要步驟添加適當的注釋來使代碼不言自明,如果您還需要更多詳細信息,請告訴我

# 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