[英]Where should i put my images in my iPhone project?
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];
}
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];
there is no prbm with ur code. 您的代码没有prbm。 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 但是有些时候图像损坏了,由于这个原因它无法显示,请尝试其他图像,如果图像没有在束路径中添加,请在
project target > build phase > copy bundle Resources 项目目标>构建阶段>复制包资源
. 。 might be it will help. 可能会有所帮助。
You have problem at this statement 你在这句话有问题
[image release];
The easiest way to remember when to use release is NARC
keyword :) 记住何时使用release的最简单方法是NARC
关键字:)
Always remember to make release call in 4 cases. 始终记得在4种情况下拨打释放电话。
N New N新
A Alloc 一个Alloc
R Retain R保留
C Copy C副本
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. 尝试删除方法的第一行,如果您已在xib中连接了UIImageView
,则无需重新分配它。 Moreover call self.imageView.image
instead just imageView.image
. 此外致电self.imageView.image
而不只是imageView.image
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.