我是编程和开发我的第一个应用程序的新手,但我想知道如何将图像显示到imageView。
答案 0 :(得分:7)
以编程方式:
来自http://www.iphoneexamples.com/:
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
其中myImage.png是一个png格式图像,您将导入资源文件夹。
使用Nib(Xib文件):
答案 1 :(得分:1)
// First way
myImage.image = [UIImage imageNamed:@"myImage.png"];
// Second way
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/myImage.png"];
myImage.image = [UIImage imageWithContentsOfFile:fullpath];
查看 UIImageView 类引用here
答案 2 :(得分:0)
尝试使用:
imgView.image = [UIImage imageNamed:@"test.png"];
答案 3 :(得分:0)
阅读http://www.iphoneexamples.com/
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
答案 4 :(得分:0)
UIImageView *imageView=[[UIImageView alloc] init];
[imageView setImage:[UIImage imageNamed:@"yourImage.png"]];
(如果您已从IB创建了imageView并已将其插入,则忽略第一行)