简体   繁体   English

iOS:如何将图片动态添加到父视图?

[英]iOS: How to dynamically add image to a parent view?

I've got a main image on the screen, and have an "overlay" UIView placed on top of it with the background color set to Clear Color. 我在屏幕上有一个主图像,并在其顶部放置了一个“覆盖” UIView,并将背景色设置为“清除颜色”。 I want to dynamically place other images inside the overlay view as follows: 我想动态地将其他图像放置在叠加视图内,如下所示:

UIImage* m_icon = [[UIImage alloc]initWithContentsOfFile:@"icon.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:m_icon];
[m_OverlayView addSubview:imageView];

But the icon image isn't showing up. 但是没有显示图标图像。 What am I missing? 我想念什么?

You may try: 您可以尝试:

NSString *icon_path = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"png"];
UIImage* m_icon = [[UIImage alloc]initWithContentsOfFile:icon_path]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:m_icon];
[m_OverlayView addSubview:imageView];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM