How to load specific image from assets with Swift

IosImageSwiftSizeAssets

Ios Problem Overview


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/[email protected]")

Is it possible?

Ios Solutions


Solution 1 - Ios

You cannot load images directly with @2x or @3x, system selects appropriate image automatically, just specify the name using UIImage:

UIImage(named: "green-square-Retina")

Solution 2 - Ios

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

enter image description here

Solution 3 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGhostView Question on Stackoverflow
Solution 1 - IosAzatView Answer on Stackoverflow
Solution 2 - IosMaxim KholyavkinView Answer on Stackoverflow
Solution 3 - IosRoohulView Answer on Stackoverflow