【发布时间】:2012-06-06 10:56:42
【问题描述】:
我是 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];
}
【问题讨论】:
-
尝试添加到支持文件夹中
-
之前不分配镜像是无法释放的。你如何管理 imageView?通过代码还是 xib?
-
不 [UIImage imageNamed:@"1.png"] 分配内存本身吗?我的 imageView 被定义为 IBOutlet 并从 xib 连接到 FileOwner。
-
@SimonePistecchia 我不分配它,但它不会抛出异常.....
-
imageNamed返回一个自动释放的对象,因此您不必释放它。深入了解this。
标签: iphone objective-c ios xcode ios4