ios 以编程方式访问资产目录

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

Access Asset Catalog programmatically

iosobjective-cxcodeassets

提问by RileyE

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"];

[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" : "[email protected]"
    }
  ],
  "info" : {
    "version" : 1,
    "author" : "xcode"
  }
}

I'm still unsure of how I should be accessing it, but maybe this will help?

我仍然不确定我应该如何访问它,但也许这会有所帮助?

回答by RileyE

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 @"[email protected]"to the Asset Catalog, it will create an asset group called my-button.

因此,如果您将名为@"[email protected]"资产目录的图像添加到资产目录,它将创建一个名为 的资产组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 UIScreenscales without any scalechecks.

此外,您可以通过重命名(无需重命名图像)或更改其单个组件来编辑资产组。这将允许您遵循更简单的命名约定,并在不同UIScreenscales之间显示完全不同的资产,而无需任何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).

为了合并不同设备尺寸的图像,您可能需要在资产目录组选项中的“设备”副标题下切换它。这是该切换的示例(可通过右键单击组获得)。

回答by Yigit Yilmaz

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

图像文字

回答by Suragch

Swift

迅速

You can get a reference to an image in your asset catelog with

您可以使用以下命令获取对资产类别中图像的引用

UIImage(named: "myImageName")

You don't need to include the extension.

您不需要包含扩展名。

回答by AppHandwerker

@RileyE is 100% right. 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. But when you reference it from code it's not so obvious what the problem is.

@RileyE 是 100% 正确的。但是,根据我的经验还值得注意的是,有时图像的资产目录引用可能包含尾随空格。您可能不会注意到是否使用 storyboards/xibs 作为自动完成会添加它。但是当你从代码中引用它时,问题是什么就不那么明显了。