简体   繁体   English

创建UIImageView

[英]create UIImageView

如何在代码中创建UIImageView ,而不是在xib文件中创建UIImageView

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];
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 您可能也想查看Apple文档: http : //developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html

到目前为止,答案比它们需要的更为复杂:initWithImage:“调整接收器的帧以匹配指定图像的大小。”因此,您所需要做的就是

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

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

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