如何使用Swift从资产加载特定映像[副本]

时间:2022-02-23 00:26:19

This question already has an answer here:

这个问题已经有了答案:

I'm new to Swift and I want to load a special image from assets. For example I have:

我是Swift新手,我想从assets加载一个特殊的图像。例如我有:

image 1 for iphone 4s = green-square@2x.png
image 2 for iphone 5/5s = green-square-Retina@2x.png
image 3 for iphone 6s = green-square@3x.png

and I want to load for iphone 6 a specific image like

我想为iphone 6加载一个特定的图像

self.GSquare = SKSpriteNode(imageNamed: "./Images.xcassets/green-square-Retina@2x.png")

Is it possible?

是可能的吗?

3 个解决方案

#1


66  

You shouldn't load images directly with @2x or @3x, system selects appropriate image automatically, you only need to specify the name, like UIImage(named: "green-square-Retina")

你不应该直接使用@2x或@3x来加载图像,系统自动选择合适的图像,你只需要指定名称,比如UIImage(命名为:“绿方视网膜”)

#2


11  

Since swift 3.0 there is more convenient way: #imageLiterals here is text example. And below animated example from here:

由于swift 3.0有更方便的方式:#imageLiterals这里是文本示例。下面是这里的动画例子:

如何使用Swift从资产加载特定映像[副本]

#3


0  

You can easily pick image from asset without UIImage(named: "green-square-Retina").

在没有UIImage的情况下,您可以很容易地从asset中选择image(命名为:“green-square-Retina”)。

Instead use the image object directly from bundle.
Start typing the image name and you will get suggestions with actual image from bundle. It is advisable practice and less prone to error.

而是直接从bundle中使用image对象。开始输入图像名称,您将从bundle获得实际图像的建议。这是明智的做法,不容易出错。

See this * answer for reference.

参见这个*答案以供参考。

#1


66  

You shouldn't load images directly with @2x or @3x, system selects appropriate image automatically, you only need to specify the name, like UIImage(named: "green-square-Retina")

你不应该直接使用@2x或@3x来加载图像,系统自动选择合适的图像,你只需要指定名称,比如UIImage(命名为:“绿方视网膜”)

#2


11  

Since swift 3.0 there is more convenient way: #imageLiterals here is text example. And below animated example from here:

由于swift 3.0有更方便的方式:#imageLiterals这里是文本示例。下面是这里的动画例子:

如何使用Swift从资产加载特定映像[副本]

#3


0  

You can easily pick image from asset without UIImage(named: "green-square-Retina").

在没有UIImage的情况下,您可以很容易地从asset中选择image(命名为:“green-square-Retina”)。

Instead use the image object directly from bundle.
Start typing the image name and you will get suggestions with actual image from bundle. It is advisable practice and less prone to error.

而是直接从bundle中使用image对象。开始输入图像名称,您将从bundle获得实际图像的建议。这是明智的做法,不容易出错。

See this * answer for reference.

参见这个*答案以供参考。