ios 如何使用 Swift 从资产加载特定图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29356574/
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
How to load specific image from assets with Swift
提问by Ghost
I'm new to Swift and I want to load a special image from assets. For example I have:
我是 Swift 的新手,我想从资产加载特殊图像。例如我有:
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
我想为 iphone 6 加载一个特定的图像,比如
self.GSquare = SKSpriteNode(imageNamed: "./Images.xcassets/[email protected]")
Is it possible?
是否可以?
回答by Azat
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
or加载图片@3x
,系统会自动选择合适的图片,你只需要指定名称,比如UIImage(named: "green-square-Retina")
回答by Maxim Kholyavkin
回答by Roohul
You can easily pick image from asset without UIImage(named: "green-square-Retina")
.
您可以轻松地从资产中选择图像,而无需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 thisStackoverflow answer for reference.
请参阅此Stackoverflow 答案以供参考。