简体   繁体   English

Xcode资产目录:每个图像只有一个副本

[英]Xcode Asset Catalog: Only one copy of each image

So I created an Asset Catalog with four positions: two for iPhone and two for iPad. 所以我创建了一个有四个位置的资产目录:两个用于iPhone,两个用于iPad。 Now I drag'n'drop my images to the corresponding positions. 现在我把我的图像拖到相应的位置。

My Problem: Let's say I use the same image for iPhone @2x and iPad @1x. 我的问题:假设我在iPhone @ 2x和iPad @ 1x上使用相同的图像。 But Xcode creates a copy of the image, although the exact same image already exists. 但Xcode会创建图像的副本,尽管已经存在完全相同的图像。

So I tried solving this problem manually thanks to this answer . 所以我试着通过这个答案手动解决这个问题。 I open the .imageset folder and edit the "filename" in the Contents.json file like this: (I also delete the duplicate image in the folder.) 我打开.imageset文件夹并在Contents.json文件中编辑“filename”,如下所示:(我还删除了文件夹中的重复图像。)

{
  "images" : [
    {
      "idiom" : "iphone",
      "scale" : "1x"
      "filename" : "myImage@1x.png"
    },
    {
      "idiom" : "iphone",
      "scale" : "2x",
      "filename" : "myImage@2x.png"
    },
    {
      "idiom" : "ipad",
      "scale" : "1x",
      "filename" : "myImage@2x.png"
    },
    {
      "idiom" : "ipad",
      "scale" : "2x",
      "filename" : "myImage@4x.png"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

After the build I take a look inside the .app file and at the Assets.car file, which contains the Xcode Asset Catalog. 在构建之后,我查看.app文件和Assets.car文件,其中包含Xcode资产目录。

The file size of the Assets.car file seems to be larger than I expected. Assets.car文件的文件大小似乎比我预期的大。 As if the duplicate image I deleted was re-added in the build process. 好像我删除的重复图像在构建过程中重新添加。

To see if this is true, I changed every "filename" in the Contents.json to my @4x image: 要查看是否为真,我将Contents.json中的每个“文件名”更改为我的@ 4x图像:

{
  "images" : [
    {
      "idiom" : "iphone",
      "scale" : "1x"
      "filename" : "myImage@4x.png"
    },
    {
      "idiom" : "iphone",
      "scale" : "2x",
      "filename" : "myImage@4x.png"
    },
    {
      "idiom" : "ipad",
      "scale" : "1x",
      "filename" : "myImage@4x.png"
    },
    {
      "idiom" : "ipad",
      "scale" : "2x",
      "filename" : "myImage@4x.png"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

After the build, I checked the file size fo the Assets.car. 在构建之后,我检查了Assets.car的文件大小。 And it was significantly larger than before. 它比以前大得多。 Approximately four times the size of "myImage@4x.png" – although in my .imageset folder, there was only one "myImage@4x.png". 大约是“myImage@4x.png”大小的四倍 - 虽然在我的.imageset文件夹中,只有一个“myImage@4x.png”。 So I assume Xcode added three copies of that image during the build process. 所以我假设Xcode在构建过程中添加了该图像的三个副本。

Does anyone know how to stop Xcode from adding these unnecessary copies? 有谁知道如何阻止Xcode添加这些不必要的副本? If your app contains many Asset Catalogs, the app size could increase significantly... 如果您的应用包含许多资产目录,那么应用规模可能会显着增加......

Thanks in advance for your help! 在此先感谢您的帮助!

Slicing is the process of creating and delivering variants of the app bundle for different target devices. 切片是为不同的目标设备创建和交付应用程序包变体的过程。

from https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/AppThinning/AppThinning.html 来自https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/AppThinning/AppThinning.html

App slicing prevents the app size to increase, because only the device specific resources are downloaded to the customers device. 应用程序切片可防止应用程序大小增加,因为只有特定于设备的资源才会下载到客户设备。

Regarding the upload size from a developer perspective your worries seem to be true. 从开发人员的角度来看,您的担忧似乎是正确的。

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

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