简体   繁体   English

以编程方式访问资产目录

[英]Access Asset Catalog programmatically

I know it's a new feature and this may not be possible, but I would love to be able to use an Asset Catalog to organize my assets, but I access all of my images programmatically. 我知道这是一个新功能,这可能是不可能的,但我希望能够使用资产目录来组织我的资产,但我以编程方式访问我的所有图像。 How would I access my images, now? 我现在如何访问我的图片? Do I still access them by their file names like so: 我仍然可以通过文件名访问它们,如下所示:

[UIImage imageNamed:@"my-asset-name.png"];

Seemingly, the Asset Catalog doesn't reference the extension, so would it be more efficient to access it without the ".png"? 看起来,资产目录没有引用扩展名,所以在没有“.png”的情况下访问它会更有效吗?

The reason I am asking instead of testing for myself is that even after removing my assets and Asset Catalog, then cleaning the build folder, I can still access my assets in my application. 我要求而不是自己测试的原因是,即使删除了我的资产和资产目录,然后清理了构建文件夹,我仍然可以在我的应用程序中访问我的资产。 This is preventing me from testing the Asset Catalog, when I implement it. 当我实现它时,这阻止我测试资产目录。

After looking through the Asset Catalog, I found the "Contents.json" for each asset and it's formatted like so: 浏览资产目录后,我找到了每个资产的“Contents.json”,其格式如下:

{
  "images" : [
    {
      "idiom" : "universal",
      "scale" : "1x"
    },
    {
      "idiom" : "universal",
      "scale" : "2x",
      "filename" : "my-asset@2x.png"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

I'm still unsure of how I should be accessing it, but maybe this will help? 我仍然不确定我应该如何访问它,但也许这会有所帮助?

In order to access the image from the Asset Catalog, you only need to access the name of the asset group without any extensions. 要从资产目录访问图像,您只需访问资产组的名称而无需任何扩展名。

So, if you add an image named @"my-button@2x.png" to the Asset Catalog, it will create an asset group called my-button . 因此,如果将名为@"my-button@2x.png"的图像添加到资产目录,它将创建一个名为my-button的资产组。

Now, all you have to do is access the image like so: 现在,您所要做的就是访问图像,如下所示:

// Objective-C
[UIImage imageNamed:@"my-button"];
// Swift
UIImage(named: "my-button")

Also, you can edit the asset group by renaming it (without renaming the images) or changing it's individual components. 此外,您可以通过重命名资产组(不重命名图像)或更改其各个组件来编辑资产组。 This will allow you to follow easier naming conventions as well as show completely different assets between different UIScreen scale s without any scale checks. 这将允许您遵循更简单的命名约定,并在不同的UIScreen scale之间显示完全不同的资产,而无需任何scale检查。

In order to incorporate images for different device sizes, you may need to toggle it under the "Devices" subheading in the Asset Catalog Group's options. 为了合并不同设备尺寸的图像,您可能需要在资产目录组选项的“设备”子标题下进行切换。 Here is an example of that toggle (available by right clicking the group). 以下是切换的示例(通过右键单击组可用)。

Also Apple has added new way to get image from assets with Swift 3, It is calling as 'Image Literal' and work as below: Apple还增加了使用Swift 3从资产中获取图像的新方法,它称之为“Image Literal” ,其工作方式如下:

Image Literal

Swift 迅速

You can get a reference to an image in your asset catelog with 您可以在资产catelog中获取对图像的引用

UIImage(named: "myImageName")

You don't need to include the extension. 您不需要包含扩展名。

@RileyE is 100% right. @RileyE 100%正确。 However, from my experiences It's also worth noting that sometimes the asset catalogue reference for the image can contain trailing white space. 但是,根据我的经验值得注意的是,有时图像的资产目录引用可能包含尾随空格。 You probably wouldn't notice if using storyboards/xibs as the auto complete will add it. 你可能不会注意到如果使用storyboards / xibs,因为自动完成将添加它。 But when you reference it from code it's not so obvious what the problem is. 但是当你从代码中引用它时,问题就不那么明显了。

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

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