如何将 JPEG 添加到 Xcode 中的资产目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18785946/
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 04:54:36  来源:igfitidea点击:

How can I add JPEGs to an asset catalog in Xcode?

xcodeassetsxcode5

提问by David Snabel-Caunt

Is it possible to add assets other than PNG files to an Xcode Asset Catalog?

是否可以将 PNG 文件以外的资产添加到 Xcode 资产目录?

When I drag JPEG files into an Asset Catalog they aren't accepted by the UI.

当我将 JPEG 文件拖入资产目录时,UI 不接受它们。

采纳答案by David Snabel-Caunt

You can add non-PNG assets by editing the JSON representation of the asset manually. The easiest way is to copy an existing asset and modify it:

您可以通过手动编辑资产的 JSON 表示来添加非 PNG 资产。最简单的方法是复制现有资产并修改它:

  1. Right click on an existing asset and choose Show in Finder
  2. Copy and paste the existing .imagesetitem and rename it, e.g. my_image.imageset
  3. Double-click the new .imageset
  4. Delete any existing images in the folder
  5. Copy in your JPEG files
  6. Edit the Contents.jsonfile, replacing the values for the filenamekey with your JPEG filenames
  1. 右键单击现有资产并选择在 Finder 中显示
  2. 复制并粘贴现有.imageset项目并重命名,例如my_image.imageset
  3. 双击新建 .imageset
  4. 删除文件夹中的所有现有图像
  5. 复制您的 JPEG 文件
  6. 编辑Contents.json文件,用filename您的 JPEG 文件名替换键的值

Your Contents.jsonwill look something like this:

Contents.json会看起来像这样:

{
  "images" : [
    {
      "idiom" : "universal",
      "scale" : "1x",
      "filename" : "my_image.jpg"
    },
    {
      "idiom" : "universal",
      "scale" : "2x",
      "filename" : "[email protected]"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

Be sure to refer to your image by name, without extension:

请务必按名称引用您的图像,不带扩展名:

[UIImage imageNamed:@"my_image"]

This approach will work for GIFs and other assets as they are just copied into the App's main bundle at build time. It is worth noting that the images end up with a png extension when copied to the bundle, but they still load correctly.

这种方法适用于 GIF 和其他资产,因为它们只是在构建时复制到应用程序的主包中。值得注意的是,图像在复制到捆绑包时最终带有 png 扩展名,但它们仍然可以正确加载。

回答by Kevin Sylvestre

As of Xcode 6.1 JPG images can now be added to an asset catalog. The steps to add are:

从 Xcode 6.1 开始,现在可以将 JPG 图像添加到资产目录中。添加步骤如下:

  1. Export your JPG at the desired quality using the same naming conventions as for PNGs (i.e. heart.jpg[email protected][email protected])
  2. Drag the JPGs into your asset catalogue from Finder and add any splicing or device specific settings to the asset
  3. Change the 'Render As' to be 'Original Image' for all JPGs (otherwise they'll appear blank in the simulator and on the device)
  1. 使用与 PNG 相同的命名约定以所需质量导出 JPG(即heart.jpg[email protected][email protected]
  2. 将 JPG 从 Finder 拖到您的资产目录中,并将任何拼接或设备特定设置添加到资产
  3. 将所有 JPG 的“渲染为”更改为“原始图像”(否则它们将在模拟器和设备上显示为空白)

回答by onbek

It appears you can now drag and drop JPEGs in Xcode 6.0 Beta

看来您现在可以在 Xcode 6.0 Beta 中拖放 JPEG

回答by gavdotnet

In Xcode 6.x you can drag and drop jpegs to the asset catalog. If creating a UIImage from the asset make sure you use the .jpg extension like so:

在 Xcode 6.x 中,您可以将 jpeg 拖放到资产目录中。如果从资产创建 UIImage,请确保使用 .jpg 扩展名,如下所示:

[UIImage imageNamed:@"myimage.jpg"];

If the extension is not included, the image will just appear white/blank.

如果不包含扩展名,图像将仅显示为白色/空白。

回答by Carlos Parada

You can also rename your image.jpg to image.png if you don't want to change the Contents.json file. You can then add the images to the asset catalog, even though internally they are still jpeg files. You can even slice them using Xcode.

如果您不想更改 Contents.json 文件,也可以将 image.jpg 重命名为 image.png。然后您可以将图像添加到资产目录中,即使它们在内部仍然是 jpeg 文件。您甚至可以使用 Xcode 对它们进行切片。

When the application is compiled, all assets go into the Assets.car file. I haven't verified if at this point they get converted to png.

编译应用程序时,所有资产都进入 Assets.car 文件。我还没有验证此时它们是否被转换为 png。

回答by Seamus

Try this. Quickly import resource scripts to resolve import speed and rename issues. https://github.com/qdvictory/happyxcasset

尝试这个。快速导入资源脚本,解决导入速度和重命名问题。 https://github.com/qdvictory/happyxcasset