How do I create a UIImageView
in code, instead of creating it in my xib file?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
UIImageView *someImageView = [[UIImageView alloc] initWithFrame:someRect];
someImageView.image = someImage;
[self.view addSubview:someImageView];
Easy as pie! :D
You might wanna check the apple docs too: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html
回答2:
You can create seperately a UIImage, and create your UIImageView from your UIImage.
UIImage *myImage = [UIImage imageNamed:@"great_pic.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage];
Then, set the size of your 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];
回答3:
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
UIImageView* v = [[UIImageView alloc] initWithImage: someImage];
回答4:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 7, 190, 23];
tabNameBackGround.image = [UIImage imageNamed:@"a.png"];
[self.view addSubview:imageView];