xcode 如何在 Swift 中显示 UIImage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30096087/
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 display an UIImage in Swift
提问by mcfly soft
I simply would like to display an image which is part of the app.
When executing the following code, my image is nil
.
我只是想显示作为应用程序一部分的图像。执行以下代码时,我的图像是nil
.
let image = UIImage(named: "back.png");
The "back.png" is stored in my "Images.xcassets"
folder of my project.
“back.png”存储在我"Images.xcassets"
的项目文件夹中。
How to load the image from within the app folders?
如何从应用程序文件夹中加载图像?
(Loading the Image from a path is working for me, but I do not know how to reference an image stored in the project itself)
(从路径加载图像对我有用,但我不知道如何引用存储在项目本身中的图像)
回答by ABakerSmith
You need to use the name of the Image Set, inside Images.xcassets
that back.png
is stored in, instead of the actual file name.
您需要使用的映像集,里面的名称Images.xcassets
是back.png
存储在,而不是实际的文件名。
From the Asset Catalog above (obviously there would be some images in there though), you would use the code:
从上面的资产目录(显然那里会有一些图像),您将使用以下代码:
let image = UIImage(named: "Image")
Have a look at About Asset Catalogsfor more information.
查看关于资产目录了解更多信息。
回答by swiftBoy
回答by Aleksey Kornienko
Don't use file extension if you place image in Images.xcassets. You need to use the name of the Image Set.
如果将图像放置在 Images.xcassets 中,请不要使用文件扩展名。您需要使用图像集的名称。
let image = UIImage(named: "back")