create UIImageView

痴心易碎 提交于 2019-12-10 04:10:04

问题


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];


来源:https://stackoverflow.com/questions/7891613/create-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!