How to load specific image from assets with Swift

2019-03-17 02:55发布

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:

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

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

Is it possible?

3条回答
贪生不怕死
2楼-- · 2019-03-17 03:07

You can easily pick image from asset without UIImage(named: "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.

See this Stackoverflow answer for reference.

查看更多
甜甜的少女心
3楼-- · 2019-03-17 03:14

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

enter image description here

查看更多
Fickle 薄情
4楼-- · 2019-03-17 03:29

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")

查看更多
登录 后发表回答