ios 创建 UIImageView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7891613/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:03:16  来源:igfitidea点击:

create UIImageView

objective-cioscocoa-touchuiimageview

提问by gadgetmo

How do I create a UIImageViewin code, instead of creating it in my xib file?

如何UIImageView在代码中创建一个,而不是在我的 xib 文件中创建它?

回答by NSZombie

You can create seperately a UIImage, and create your UIImageView from your UIImage.

您可以单独创建一个 UIImage,并从您的 UIImage 创建您的 UIImageView。

UIImage *myImage = [UIImage imageNamed:@"great_pic.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage];

Then, set the size of your UIImageView

然后,设置 UIImageView 的大小

[myImageView setFrame:CGRectMake(0, 0, 100, 200)];

Do whatever you want with your image, but don't forget to release it.

对图像做任何你想做的事情,但不要忘记释放它。

[anotherView addSubview:myImageView];
[myImageView release];

回答by Totumus Maximus

UIImageView *someImageView = [[UIImageView alloc] initWithFrame:someRect];
someImageView.image = someImage;
[self.view addSubview:someImageView];

Easy as pie! :D

非常简单!:D

You might wanna check the apple docs too: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html

您可能也想查看苹果文档:http: //developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html

回答by David Dunham

The answers so far are more complex than they need to be: initWithImage: “adjusts the frame of the receiver to match the size of the specified image.” So all you need is

到目前为止,答案比他们需要的要复杂:initWithImage:“调整接收器的帧以匹配指定图像的大小。” 所以你只需要

UIImageView* v = [[UIImageView alloc] initWithImage: someImage];

回答by Tendulkar

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 7, 190, 23];
tabNameBackGround.image = [UIImage imageNamed:@"a.png"];
[self.view addSubview:imageView];