Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load specific image from assets with Swift [duplicate]

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

image 1 for iphone 4s = [email protected] image 2 for iphone 5/5s = [email protected] image 3 for iphone 6s = [email protected] 

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

self.GSquare = SKSpriteNode(imageNamed: "./Images.xcassets/[email protected]") 

Is it possible?

like image 757
Ghost Avatar asked Mar 30 '15 21:03

Ghost


People also ask

How do I assign an image in Swift?

Drag and drop image onto Xcode's assets catalog. Or, click on a plus button at the very bottom of the Assets navigator view and then select “New Image Set”. After that, drag and drop an image into the newly create Image Set, placing it at appropriate 1x, 2x or 3x slot.

How do I get a PHAsset photo?

Use PHAsset class methods to fetch the assets you're interested in. To prepare images for those assets, call the startCachingImagesForAssets:targetSize:contentMode:options: method with the target size, content mode, and options you plan to use when later requesting images for each individual asset.

How do I get an image in Swift?

Select New > Project... from Xcode's File menu and choose the Single View App template from the iOS > Application section. Name the project Images and set User Interface to Storyboard. Leave the checkboxes at the bottom unchecked. Tell Xcode where you would like to save the project and click the Create button.


1 Answers

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") 
like image 169
Azat Avatar answered Oct 06 '22 17:10

Azat