如何将 JPEG 添加到 Xcode 中的资产目录? [英] How can I add JPEGs to an asset catalog in Xcode?

查看:39
本文介绍了如何将 JPEG 添加到 Xcode 中的资产目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

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

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:

  1. 右键单击现有资产并选择在 Finder 中显示
  2. 复制并粘贴现有的 .imageset 项目并重命名,例如my_image.imageset
  3. 双击新的.imageset
  4. 删除文件夹中的任何现有图像
  5. 复制您的 JPEG 文件
  6. 编辑 Contents.json 文件,将 filename 键的值替换为您的 JPEG 文件名
  1. Right click on an existing asset and choose Show in Finder
  2. Copy and paste the existing .imageset item 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.json file, replacing the values for the filename key with your JPEG filenames

您的 Contents.json 将如下所示:

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

请务必按名称引用您的图片,不要使用扩展名:

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

[UIImage imageNamed:@"my_image"]

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

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.

这篇关于如何将 JPEG 添加到 Xcode 中的资产目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆