I'm new to iPhone dev.
我是iPhone开发人员的新手。
Now i have a project and its directory structure looks like this :
现在我有一个项目,其目录结构如下所示:
Tutorial
\__Tutorial\
\__1.png
\__TutorialViewController.h
\__TutorialViewController.m
\__TutorialViewController.xib
\__Supporting Files\
\__Frameworks\
\__Products\
I tried to use [UIImage imageNamed:@"1.png"] to render an image to the view:
我尝试使用[UIImage imageNamed:@“1.png”]将图像渲染到视图中:
UIImage *image = [UIImage imageNamed:@"1.png"];
imageView.image = image;
[image release];
But the image doesn't shows up.
但图像没有显示出来。
So i wonder where should i place the images ?
所以我想知道我应该在哪里放置图像?
And additionally, if i got lots of images (like 1000 number), what should do ?
另外,如果我有很多图像(如1000号),应该怎么办?
Here's my code :
这是我的代码:
#import "TutorialViewController.h"
@implementation TutorialViewController
@synthesize labelText;
@synthesize imageView;
-(void) click:(id)sender {
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc]initWithFormat:@"%@",titleOfButton];
// Change Image When Clicking Color Button
if([titleOfButton isEqualToString:@"Blue"]){
NSLog(@"Blue");
imageView.image = [UIImage imageNamed:@"a.png"];
}else{
imageView.image = [UIImage imageNamed:@"b.png"];;
NSLog(@"Else");
}
labelText.text = newText;
[newText release];
}
4 个解决方案
#1
1
You should create a folder named Resources and add images there.
您应该创建一个名为Resources的文件夹并在那里添加图像。
UIImage *image = [UIImage imageNamed:@"IMG_0270.PNG"];
imgView_.image = image;
I had used the above code and it works fine on my system. May be you haven't connected the outlet for imgView in the interface builder.
我使用了上面的代码,它在我的系统上工作正常。可能是您没有在界面构建器中连接imgView的插座。
EDIT:
The problem I found is you are not adding it as a subview. You should change your code like this :
我发现的问题是你没有将它添加为子视图。您应该像这样更改代码:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:@"%@", titleOfButton];
// Change Image When Clicking Color Button
if([titleOfButton isEqualToString:@"Blue"]) {
NSLog(@"Blue");
imageView.image = [UIImage imageNamed:@"IMG_0270.png"];
} else {
imageView.image = [UIImage imageNamed:@"b.png"];
NSLog(@"Else");
}
[imageView setFrame:CGRectMake(10, 10, 230, 123)];
[self.view addSubview:imageView];
// labelText.text = newText;
[newText release];
#2
0
there is no prbm with ur code. but Some time image become corrupt , due to this reason it not show, try some other images , and if image is not added at bundle path add at
你的代码没有prbm。但有些时候图像变得腐败,由于这个原因它没有显示,尝试一些其他图像,如果图像没有添加在捆绑路径添加在
project target > build phase > copy bundle Resources
项目目标>构建阶段>复制捆绑资源
. might be it will help.
。可能会有所帮助。
#3
0
You have problem at this statement
你在这个声明中有问题
[image release];
The easiest way to remember when to use release is NARC
keyword :)
记住何时使用发布的最简单方法是NARC关键字:)
Always remember to make release call in 4 cases.
永远记得在4种情况下进行释放呼叫。
N New
A Alloc
R Retain
C Copy
#4
0
Try to remove the first line of your method, if you've connected the UIImageView
in the xib you don't need to re-alloc it. Moreover call self.imageView.image
instead just imageView.image
.
尝试删除方法的第一行,如果已连接xib中的UIImageView,则不需要重新分配它。而且调用self.imageView.image而不仅仅是imageView.image。
#1
1
You should create a folder named Resources and add images there.
您应该创建一个名为Resources的文件夹并在那里添加图像。
UIImage *image = [UIImage imageNamed:@"IMG_0270.PNG"];
imgView_.image = image;
I had used the above code and it works fine on my system. May be you haven't connected the outlet for imgView in the interface builder.
我使用了上面的代码,它在我的系统上工作正常。可能是您没有在界面构建器中连接imgView的插座。
EDIT:
The problem I found is you are not adding it as a subview. You should change your code like this :
我发现的问题是你没有将它添加为子视图。您应该像这样更改代码:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:@"%@", titleOfButton];
// Change Image When Clicking Color Button
if([titleOfButton isEqualToString:@"Blue"]) {
NSLog(@"Blue");
imageView.image = [UIImage imageNamed:@"IMG_0270.png"];
} else {
imageView.image = [UIImage imageNamed:@"b.png"];
NSLog(@"Else");
}
[imageView setFrame:CGRectMake(10, 10, 230, 123)];
[self.view addSubview:imageView];
// labelText.text = newText;
[newText release];
#2
0
there is no prbm with ur code. but Some time image become corrupt , due to this reason it not show, try some other images , and if image is not added at bundle path add at
你的代码没有prbm。但有些时候图像变得腐败,由于这个原因它没有显示,尝试一些其他图像,如果图像没有添加在捆绑路径添加在
project target > build phase > copy bundle Resources
项目目标>构建阶段>复制捆绑资源
. might be it will help.
。可能会有所帮助。
#3
0
You have problem at this statement
你在这个声明中有问题
[image release];
The easiest way to remember when to use release is NARC
keyword :)
记住何时使用发布的最简单方法是NARC关键字:)
Always remember to make release call in 4 cases.
永远记得在4种情况下进行释放呼叫。
N New
A Alloc
R Retain
C Copy
#4
0
Try to remove the first line of your method, if you've connected the UIImageView
in the xib you don't need to re-alloc it. Moreover call self.imageView.image
instead just imageView.image
.
尝试删除方法的第一行,如果已连接xib中的UIImageView,则不需要重新分配它。而且调用self.imageView.image而不仅仅是imageView.image。