我是 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];
}
您应该创建一个名为 Resources 的文件夹并在其中添加图像。
UIImage *image = [UIImage imageNamed:@"IMG_0270.PNG"];
imgView_.image = image;
我使用了上面的代码,它在我的系统上运行良好。可能是你没有在界面生成器中连接imgView的插座。
EDIT:
我发现的问题是您没有将其添加为子视图。你应该像这样改变你的代码:
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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)