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

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

问题描述

是否可以将除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文件,用您的JPEG文件名替换filename键的值
  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和其他资产,因为它们是在构建时被复制到App的主捆绑包中的.值得注意的是,复制到捆绑包中的图像最终以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.

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

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