2012-06-06 198 views
2

我是iPhone开发新手。我应该在哪里把我的图片放在我的iPhone项目中?

现在我有一个项目,它的目录结构如下:

Tutorial 
     \__Tutorial\ 
       \__1.png 
       \__TutorialViewController.h 
       \__TutorialViewController.m 
       \__TutorialViewController.xib 
       \__Supporting Files\ 
     \__Frameworks\ 
     \__Products\ 

我试图用[UIImage的imageNamed:@“1.png”]来渲染视图图像:

UIImage *image = [UIImage imageNamed:@"1.png"]; 
imageView.image = image; 
[image release]; 

但图像没有显示出来。

所以我想知道我应该把图像放在哪里?

此外,如果我有很多图像(如1000号),应该怎么做?


这里是我的代码:

#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]; 
} 
+3

如果您之前未分配图像,则无法释放图像。你如何管理imageView?通过代码或xib? –

+0

没有[UIImage imageNamed:@“1.png”]分配内存本身?我的imageView被定义为一个IBOutlet并从xib连接到FileOwner。 – MrROY

+0

@SimonePistecchia我不分配它,但它不会抛出异常..... – MrROY

回答

1

你应该创建一个文件夹命名为资源和添加图片在那里。

UIImage *image = [UIImage imageNamed:@"IMG_0270.PNG"]; 
imgView_.image = image; 

我用了上面的代码,它工作正常我的系统上。可能你没有在界面构建器中连接imgView的插座。

编辑:

我发现这个问题是你不将其添加为一个子视图。你应该像这样改变你的代码:

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

建议很好,但它不能解决OP的问题。 – nhahtdh

+0

@nhahtdh已更新答案 – Vaquita

+0

@MrRoy您的问题解决了吗? – Vaquita

0

没有你的代码的prbm。但经过一段时间的图像成为腐败,由于这个原因,它没有显示,尝试一些其他的图像,如果图像没有在束路径添加在

项目目标添加>建立阶段>复制包资源

。可能会有所帮助。

0

您在本声明

[image release]; 

有问题时要记住用最简单的方式释放NARC关键字:)

永远记住,使释放的通话4例。

N新

一个的Alloc

[R保留

C复制

+0

NARC,仅在没有ARC时使用发布:D – iNoob

0

尝试删除你的方法的第一行,如果你连接在UIImageView xib你不需要重新分配它。此外请致电self.imageView.image而不是imageView.image

相关问题