xcode 如何减小我的 iPhone 应用程序的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2490910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:19:28  来源:igfitidea点击:

How to reduce the size of my iPhone application?

iphonexcodeipadcompressionipa

提问by Sudharsanan

Alternative Titles (to aid searches)

替代标题(以帮助搜索)

Compressing PNGs

压缩PNG

Reduce the size of an iPhone Archive (.ipa)

减小 iPhone 档案 (.ipa) 的大小

Adding a build rule to compress images in Xcode

添加构建规则以在 Xcode 中压缩图像



iOS Apps can only be downloaded via 3G if they are less than 100MB. What are the best strategies for reducing the size of an App?

小于 100MB 的 iOS 应用程序只能通过 3G 下载。减少应用程序大小的最佳策略是什么?

Areas I'd like to focus on are:

我想关注的领域是:

  • Images
  • Databases
  • Code
  • Static Libraries
  • 图片
  • 数据库
  • 代码
  • 静态库

NB:The original question can be viewed in the revisions of this question

注意:原始问题可以在此问题的修订中查看

采纳答案by Paul Lammertsma

PNG is really the best option for lossless image compression. You can optimize your PNGs for size using PNGOUT, but may I inquire which files specifically are taking much space? How does it compare to a non-debug release?

PNG 确实是无损图像压缩的最佳选择。您可以使用PNGOUT优化 PNG 的大小,但我可以询问哪些文件特别占用了太多空间吗?它与非调试版本相比如何?

Edit: If you'd like a free GUI-version of PNGOUT, take a look at PNGGauntlet.

编辑:如果您想要 PNGOUT 的免费 G​​UI 版本,请查看PNGGauntlet

回答by Richard Stelling

Adding an Xcode Build Rule to compress PNGs

添加 Xcode 构建规则以压缩 PNG

Images in an iOS App can make up the majority of it size. This is especially true if it is a Universal App requiring it to have all image in triplicate (iPhone Standard, iPhone Retina iPad).

iOS 应用程序中的图像可以占其大小的大部分。如果它是一个要求所有图像一式三份(iPhone 标准版、iPhone Retina iPad 版)的通用应用程序,则尤其如此。

The best type of image to use in an iOS App is a PNG. When your designer created these images in Photoshop they are saved with a lot of meta data that can be discarded.

在 iOS 应用程序中使用的最佳图像类型是 PNG。当您的设计师在 Photoshop 中创建这些图像时,它们与许多可以丢弃的元数据一起保存。

However you don't want to loose all that data completely as it's useful to designers if they need to alter the image.

但是,您不想完全丢失所有数据,因为如果设计师需要更改图像,这对设计师很有用。

PNGOUT Optimisation

PNGOUT优化

There are several tools for optimising PNG files, but pngoutseems to be the best option.

有几种优化 PNG 文件的工具,但pngout似乎是最好的选择。

  1. Download the Mac Version of pngout.
  2. Copy the binary command-line application pngoutto a directory with in your project.Adding the binary to the project directory makes sure is is available to anyone building the project on any system.
  1. 下载 pngout 的 Mac 版本
  2. 将二进制命令行应用程序复制pngout到项目中的目录。将二进制文件添加到项目目录可确保任何人都可以在任何系统上构建项目。

Creating a Build Rule (Xcode 4)

创建构建规则 (Xcode 4)

Build rules are Target specific, so if you have more than one target you must copy the rule into each.

构建规则是特定于目标的,因此如果您有多个目标,则必须将规则复制到每个目标中。

  1. Add DEBUG and DISTRIBUTION macros to your build configurations.Optimising the PNGs is quite processor intense and as such you only want to do it on distribution builds. Pre-processor macrosAs you can see amongst others I have added DEBUG_BUILD=1and DISTRIBUTION_BUILD=1.

  2. Add a build rule for PNG files.A build rule simply processes a specific file (and/or) file type during the build process. The power of this is rules can be chained together. Build Rule - Xcode 4 http://tinypic.com/images/404.gif

  3. Click the "Add Rule" button;

    • Set Process to "Source files with names matching" and its value *.png.
    • Set Using to "Custom Script"
  1. 将 DEBUG 和 DISTRIBUTION 宏添加到您的构建配置中。优化 PNG 对处理器的要求非常高,因此您只想在发行版中执行此操作。预处理器宏正如您所看到的,我添加了DEBUG_BUILD=1DISTRIBUTION_BUILD=1

  2. 为 PNG 文件添加构建规则。构建规则只是在构建过程中处理特定的文件(和/或)文件类型。这是规则的力量可以链接在一起。构建规则 - Xcode 4 http://tinypic.com/images/404.gif

  3. 点击“添加规则”按钮;

    • 将 Process 设置为“名称匹配的源文件”及其值*.png
    • 将使用设置为“自定义脚本”

Paste this code into the script box

将此代码粘贴到脚本框中

echo "----------------------------------------------------------------" >> "${DERIVED_FILES_DIR}/pngout-log.txt"
echo "${INPUT_FILE_PATH}" >> "${DERIVED_FILES_DIR}/pngout-log.txt"
echo "${DERIVED_FILES_DIR}/${INPUT_FILE_NAME}" >> "${DERIVED_FILES_DIR}/pngout-log.txt"
echo ${GCC_PREPROCESSOR_DEFINITIONS} >> "${DERIVED_FILES_DIR}/pngout-log.txt"

BUILD=`echo ${GCC_PREPROCESSOR_DEFINITIONS} | grep -o DISTRIBUTION_BUILD`

echo $BUILD >> "${DERIVED_FILES_DIR}/pngout-log.txt"

if [ "${BUILD}" == "DISTRIBUTION_BUILD" ]; then
echo "COMPRESS" >> "${DERIVED_FILES_DIR}/pngout-log.txt"
"${PROJECT_DIR}/build-process/pngout" -y -q -force "${INPUT_FILE_PATH}" "${DERIVED_FILES_DIR}/${INPUT_FILE_NAME}"
else
echo "COPY" >> "${DERIVED_FILES_DIR}/pngout-log.txt"
cp -f "${INPUT_FILE_PATH}" "${DERIVED_FILES_DIR}/${INPUT_FILE_NAME}"
fi

echo "...done." >> "${DERIVED_FILES_DIR}/pngout-log.txt"

There are several environment variables that are of note:

有几个环境变量值得注意:

  • ${INPUT_FILE_PATH}— full path to the image file
  • ${INPUT_FILE_NAME}— image file name (with extension)
  • ${DERIVED_FILES_DIR}— where Xcode stores build files etc
  • ${GCC_PREPROCESSOR_DEFINITIONS}— the macros you set above
  • ${INPUT_FILE_PATH}— 图像文件的完整路径
  • ${INPUT_FILE_NAME}— 图像文件名(带扩展名)
  • ${DERIVED_FILES_DIR}— Xcode 存储构建文件等的位置
  • ${GCC_PREPROCESSOR_DEFINITIONS}——你上面设置的宏

The work is done on this line:

工作是在这条线上完成的:

    "${PROJECT_DIR}/build-process/pngout" -y -q -force "${INPUT_FILE_PATH}" "${DERIVED_FILES_DIR}/${INPUT_FILE_NAME}"

${PROJECT_DIR}is the full path to your project, the -yoverwrites existing files, -qlimits pngouts output and -forceprevents pngoutfrom exiting with status of 2 when a file can't be optimised and causing Xcode report build errors.

${PROJECT_DIR}是项目的完整路径,它会-y覆盖现有文件、-q限制pngouts 输出并-force防止pngout在文件无法优化并导致 Xcode 报告构建错误时以状态 2 退出。

This script simply tests the ${GCC_PREPROCESSOR_DEFINITIONS}to see if it a DISTRIBUTION_BUILDif so it uses pngoutto optimise the file, otherwise it copies it to the ${DERIVED_FILES_DIR}so Xcode can continue to process it.

这个脚本只是简单地测试${GCC_PREPROCESSOR_DEFINITIONS}DISTRIBUTION_BUILD是否是它pngout用来优化文件的,否则它会将它复制到${DERIVED_FILES_DIR}Xcode 可以继续处理它。

Finally, don't forget to add ${DERIVED_FILES_DIR}/${INPUT_FILE_NAME}to the "Output files" list this is how Xcode knows how to find the files you have processed.

最后,不要忘记添加${DERIVED_FILES_DIR}/${INPUT_FILE_NAME}到“输出文件”列表中,这是 Xcode 知道如何找到您处理过的文件的方式。

回答by Michael Anderson

A debug build will usually be much larger than a release build. Try building in release mode.

调试版本通常比发布版本大得多。尝试在发布模式下构建。

There's a few compiler options that may help too. Not sure whats default for iphone release mode though. -Os will optimize for a smaller binary. There's also an option for dead code stripping that will remove any code that can never be run. Also you might try stripping the binary .. not sure if that works for iphone binaries though.

还有一些编译器选项可能也有帮助。不确定 iPhone 发布模式的默认设置是什么。-Os 将针对较小的二进制文件进行优化。还有一个死代码剥离选项,可以删除任何永远无法运行的代码。您也可以尝试剥离二进制文件.. 不确定这是否适用于 iphone 二进制文件。

This is assuming that your problem is due to executable binary size and not the image resources.

这是假设您的问题是由于可执行二进制大小而不是图像资源造成的。

回答by Amit Vaghela

I used ImageOptimand ImageAlphafor reducing app size. Have a look at this casestudy for more information. Tweetbot for iPad

我使用ImageOptimImageAlpha来减小应用程序的大小。查看此案例研究以获取更多信息。适用于 iPad 的推特机器人

回答by Jakehao

I have reduce an iOS app from 90MB to 50MB in the last few monthes. Here are some tips and tools:

在过去的几个月里,我将一个 iOS 应用程序从 90MB 减少到 50MB。以下是一些提示和工具:

To Profile iOS App Size

配置 iOS 应用程序大小

  1. unzip your ipa to check resources like video, font and images files.

  2. Profile your binary file by library and object files(classes) using Linkmap file and XCode Linkmap Parser.

  1. 解压缩您的 ipa 以检查视频、字体和图像文件等资源。

  2. 使用 Linkmap 文件和XCode Linkmap Parser按库和目标文件(类)分析您的二进制文件。

To Reduce Image Files

减少图像文件

To Reduce Binary File

减少二进制文件

  • Strip debug symbols
  • Find unused imports using fui
  • 剥离调试符号
  • 使用fui查找未使用的导入

回答by btomw

@rjstelling has a really nice answer, and I couldn't begin to say it better, but there is one small problem with it. It won't work with localized images. This is only a problem if you embed text in images, which is something I highly recommend never doing if you can avoid it. However, if you have to use localized images, follow all of rjstelling 's steps except instead of using:

@rjstelling 有一个非常好的答案,我不能说得更好,但是它有一个小问题。它不适用于本地化图像。如果您在图像中嵌入文本,这只是一个问题,如果可以避免,我强烈建议您永远不要这样做。但是,如果您必须使用本地化图像,请按照 rjstelling 的所有步骤进行操作,而不是使用:

if [ "${BUILD}" == "DISTRIBUTION_BUILD" ]; then
echo "COMPRESS" >> "${DERIVED_FILES_DIR}/pngout-log.txt"
"${PROJECT_DIR}/build-process/pngout" -y -q -force "${INPUT_FILE_PATH}" "${DERIVED_FILES_DIR}/${INPUT_FILE_NAME}"
else
echo "COPY" >> "${DERIVED_FILES_DIR}/pngout-log.txt"
cp -f "${INPUT_FILE_PATH}" "${DERIVED_FILES_DIR}/${INPUT_FILE_NAME}"
fi

use the code below:

使用下面的代码:

if [ "${BUILD}" == "DISTRIBUTION_BUILD" ]; then
echo "COMPRESS" >> "${DERIVED_FILES_DIR}/pngout-log.txt"
"${PROJECT_DIR}/build-process/pngout" -y -q -force "${INPUT_FILE_PATH}" "${DERIVED_FILES_DIR}/${INPUT_FILE_REGION_PATH_COMPONENT}${INPUT_FILE_NAME}"
else
echo "COPY" >> "${DERIVED_FILES_DIR}/pngout-log.txt"
cp -f "${INPUT_FILE_PATH}" "${DERIVED_FILES_DIR}/${INPUT_FILE_REGION_PATH_COMPONENT}${INPUT_FILE_NAME}"
fi

Note the only change is to the export file path. You have to prepend INPUT_FILE_REGION_PATH_COMPONENTto the export path to get localization of image to work properly. INPUT_FILE_REGION_PATH_COMPONENTis empty normally unless there is localized folder for the specific file. That being the case, it provides the folder name and a trailing slash, so you don't have to.

请注意,唯一的更改是导出文件路径。您必须INPUT_FILE_REGION_PATH_COMPONENT在导出路径前面加上图像的本地化才能正常工作。INPUT_FILE_REGION_PATH_COMPONENT除非有特定文件的本地化文件夹,否则通常为空。在这种情况下,它提供文件夹名称和尾部斜杠,因此您不必这样做。

You must also change your Output Filessetting to ${DERIVED_FILES_DIR}/${INPUT_FILE_REGION_PATH_COMPONENT}${INPUT_FILE_NAME}in order for this to work.

您还必须将您的Output Files设置更改${DERIVED_FILES_DIR}/${INPUT_FILE_REGION_PATH_COMPONENT}${INPUT_FILE_NAME}为 以使其正常工作。

Not sure if I'm the only one who's ever had that problem, but hopefully it helps someone.

不确定我是否是唯一遇到过这个问题的人,但希望它可以帮助某人。

回答by Murat Yasar

Inspecting an .ipa File

检查 .ipa 文件

Simply change the extension of an .ipa file to .zip, then open it with Finder to decompress it. Right-click on the unzipped .app bundle and choose "Show Package Contents" to see the resources inside. This can give you an idea of what is using the most space.

You can examine the compressed size of each item in your .ipa by opening Terminal and executing the following command:

unzip -lv /path/to/your/app.ipa

The size column has the compressed size of each file within your .ipa file. You should evaluate this size as you work to reduce the size of resources in your app. You may reduce the size of a resource by a lot, but after producing an .ipa file, you may discover the compressed size of that resource has not changed as dramatically. The most effective technique for reducing the size of an .ipa file is to remove unnecessary resources.

只需将 .ipa 文件的扩展名更改为 .zip,然后使用 Finder 打开它以解压缩它。右键单击解压缩的 .app 包并选择“显示包内容”以查看其中的资源。这可以让您了解使用最多空间的内容。

您可以通过打开终端并执行以下命令来检查 .ipa 中每个项目的压缩大小:

unzip -lv /path/to/your/app.ipa

大小列包含 .ipa 文件中每个文件的压缩大小。当您努力减少应用程序中的资源大小时,您应该评估此大小。您可能会大量减少资源的大小,但是在生成 .ipa 文件后,您可能会发现该资源的压缩大小没有显着变化。减小 .ipa 文件大小的最有效技术是删除不必要的资源。

To access the full article, see the linkfrom Apple Developer Website.

要访问完整的文章,请参阅Apple 开发人员网站上的链接