xcode UIImage 视图不显示图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28449275/
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
UIImage view does not display the image
提问by sosale151
I'm having a few issues with displaying a UIImage in an app that I'm developing. The steps I took to do this are as follows:
在我正在开发的应用程序中显示 UIImage 时遇到了一些问题。我采取的步骤如下:
1 - Add a UIImageView to my main view controller in the storyboard. I did this using the interface builder.
1 - 在情节提要中将 UIImageView 添加到我的主视图控制器。我使用界面构建器完成了此操作。
2 - I then created an IBOutlet for the UIImageView in the viewcontroller.h. I ensured proper referencing of the image view from the outlet.
2 - 然后我在 viewcontroller.h 中为 UIImageView 创建了一个 IBOutlet。我确保从插座正确引用图像视图。
3 - I set the image for the UIImageView like so:
3 - 我为 UIImageView 设置图像,如下所示:
_fireIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flame_icon.png"]];
This was done within the viewDidLoad method in the viewcontroller.m file.
这是在 viewcontroller.m 文件中的 viewDidLoad 方法中完成的。
Troubleshooting:
故障排除:
- I checked whether the _fireIcon variable is being set. It is.
- I checked whether the viewDidLoad method is being called. It is.
- In addition, the image is displayed when I statically set it via the storyboard itself. But when I do it via code, it does not display.
- 我检查了 _fireIcon 变量是否正在设置。这是。
- 我检查了是否正在调用 viewDidLoad 方法。这是。
- 此外,当我通过故事板本身静态设置图像时,会显示图像。但是当我通过代码执行此操作时,它不显示。
Let me know if you need any more additional information, or code snippets.
如果您需要更多其他信息或代码片段,请告诉我。
回答by Saif
you need not to initialize an imageview as you are using Interface builder for image view
当您使用界面构建器进行图像视图时,您无需初始化图像视图
_fireIcon.image = [UIImage imageNamed:@"flame_icon.png"]; // or [_fireIcon setImage:[UIImage imageNamed:@"flame_icon.png"]];
This sets the image. When we drop the UI elements using Interface builder , we need not initialize them, as they are taken care by apple.
这将设置图像。当我们使用 Interface builder 删除 UI 元素时,我们不需要初始化它们,因为它们是由苹果处理的。
回答by orkenstein
Doing this you reset your property with newly created UIImageView
:
这样做你用新创建的重置你的财产UIImageView
:
_fireIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flame_icon.png"]];
And your reference to UIImageView
from storyboard gets compeletely lost.
Instead you want to set image:
并且您对UIImageView
from storyboard的引用完全丢失了。
相反,您要设置图像:
_fireIcon.image = [UIImage imageNamed:@"flame_icon.png"];
回答by Tech_Intelliswift
Firstly, you do need to allocate the Element created via Interface builder, you just need to set the image to UIImageView directly like this :-
首先,您需要分配通过 Interface builder 创建的 Element,您只需要像这样直接将图像设置为 UIImageView :-
[myImage setImage:[UIImage imageNamed:@"5.jpg"]];
Make sure image name is correct and its extension too. I just tried and its working great here. Let me know if you still face problem here.
确保图像名称正确及其扩展名也是正确的。我刚刚尝试过,它在这里工作得很好。如果您在这里仍然遇到问题,请告诉我。
回答by Koushik
Swift 3.0
斯威夫特 3.0
statusImageView?.image = UIImage(named: "select.png")
statusImageView?.image = UIImage(named: "select.png")