Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从XCode项目中删除未使用的资源_Ios_Xcode - Fatal编程技术网

Ios 从XCode项目中删除未使用的资源

Ios 从XCode项目中删除未使用的资源,ios,xcode,Ios,Xcode,我正在开发iOs应用程序,在这个应用程序中,我每天在应用程序设计上都有很大的变化,所以我的应用程序的代码将达到大约200 Mb。但在这段代码中,我有大量未使用的图像可用。所以我想从我的XCode项目中删除这些图像,这样我就可以减少我的项目代码大小 我使用了stackoverflow.com上已经给出的一些脚本,但我发现脚本删除使用过的图像也是不可靠的。 我还使用了名为“苗条”的应用程序。它真的很好,但它的试用版,所以我不能使用它更多 所以,请任何人建议我有效的方法(任何应用程序)删除未使用的图像

我正在开发iOs应用程序,在这个应用程序中,我每天在应用程序设计上都有很大的变化,所以我的应用程序的代码将达到大约200 Mb。但在这段代码中,我有大量未使用的图像可用。所以我想从我的XCode项目中删除这些图像,这样我就可以减少我的项目代码大小

我使用了stackoverflow.com上已经给出的一些脚本,但我发现脚本删除使用过的图像也是不可靠的。 我还使用了名为“苗条”的应用程序。它真的很好,但它的试用版,所以我不能使用它更多


所以,请任何人建议我有效的方法(任何应用程序)删除未使用的图像从xCode项目

未使用将帮助您在xCode项目中查找未使用的资源

我在这里回答了一个类似的问题:

在此处添加相同的详细信息:

我创建了一个python脚本来标识未使用的图像:。它可以这样使用:

python3 unused_assets.py '/Users/DevK/MyProject' '/Users/DevK/MyProject/MyProject/Assets/Assets.xcassets'
以下是使用脚本的几个规则:

  • 将项目文件夹路径作为第一个参数传递给资产是很重要的 文件夹路径作为第二个参数
  • 假设所有图像都保存在Assets.xcsets文件夹中,并在swift文件或故事板中使用
第一版中的限制:

  • 不适用于目标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

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!")