如何在 Xcode 中以编程方式创建 UIImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20821465/
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 do you create a UIImageView programmatically in Xcode
提问by user3143098
I just want to make an UIImageView programmatically that displays a 20by20 dot at point (50,50).(The image of the dot is called draw.png). For some reason nothing shows up on the screen.
我只想以编程方式制作一个 UIImageView,在点 (50,50) 处显示一个 20×20 的点。(点的图像称为 draw.png)。出于某种原因,屏幕上没有显示任何内容。
Heres my code:
这是我的代码:
- (void)viewDidLoad
{
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
dot.image=[UIImage imageNamed:@"draw.png"];
[self.view addSubview:dot];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
}
回答by JuJoDi
First, make sure draw.png exists in your project and that you are referencing it exactly (so if it's draw.PNG you should put @"draw.PNG"
). Also, call [super viewDidLoad]
before everything.
首先,确保您的项目中存在 draw.png 并且您正在准确地引用它(因此如果它是 draw.PNG,您应该放置@"draw.PNG"
)。另外,[super viewDidLoad]
在一切之前打电话。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
dot.image=[UIImage imageNamed:@"draw.png"];
[self.view addSubview:dot];
}
回答by Nilesh Parmar
Try this :-
尝试这个 :-
UIImageView *imageview = [[UIImageView alloc]
initWithFrame:CGRectMake(50, 50, 20, 20)];
[imageview setImage:[UIImage imageNamed:@"AppleUSA1.jpg"]];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:imageview];
回答by Macondo2Seattle
This code is correct. Make sure draw.png
exists in your project. You can do it by checking if [UIImage imageNamed:@"draw.png"]
doesn't return nil
.
这段代码是正确的。确保draw.png
存在于您的项目中。您可以通过检查 if[UIImage imageNamed:@"draw.png"]
不返回来做到这一点nil
。
Also, make sure you don't have another view on top of your image view.
另外,请确保您的图像视图顶部没有其他视图。
回答by krushnsinh
make sure that image is in your project..
确保该图像在您的项目中..
UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
[imgView setImage:[UIImage imageNamed:@"xyz.jpg"]];//if your images extension is .png than no need to write extension of an image..
[self.view addSubview:imgView];
回答by chimbu
In Swift :
在斯威夫特:
let imageView = UIImageView(frame: CGRect(x: 50, y: 50, width: 20, height: 20))
imageView.image = UIImage(named: "draw.png")
self.view.addSubview(imageView)
回答by Kupendiran iOS
This code is perfect match an UIImageView:
此代码与 UIImageView 完美匹配:
UIImageView *IMG_view= [[UIImageView alloc] initWithFrame:CGRectMake(20 ,50 ,40 ,40)];
[IMG_view setTag:100]; //[IMG_view setTag:indexPath.row];
IMG_view.layer.borderWidth= 0.5 ;
IMG_view.layer.borderColor= [[UIColor clearColor] CGColor];
IMG_view.layer.cornerRadius= 3;
IMG_view.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:IMG_view];
IMG_view.image=[UIImage imageNamed:@"Loading_50x50.png"];