简体   繁体   English

我应该将图像放在iPhone项目的哪里?

[英]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.

相关问题 我应该在iPhone应用程序的哪里保存图像? - Where should I save images in my iPhone app? 我应该将NSFetchedResultsControllerDelegate放在哪里? - Where should I put my NSFetchedResultsControllerDelegate? iPhone,我应该在哪里将控件填充到推入视图中,viewDidLoad调用一次? - iPhone, where should I put my control populate pushed view, viewDidLoad called once? 我应该在哪里存储我的iPhone应用程序的SQLite数据库? - Where should I store the SQLite DB for my iPhone app? iPhone MVC应用程序:我应该在哪里放置模型? - iPhone MVC app: Where should I put the model? 我应该将我的 Android 应用程序移植到 iPhone 上吗? - Should I port my Android app to the iPhone? 我应该在iPhone视图中“保存”更改为“创建新对象”? - Where should I “save” changes in my iPhone view to 'create new' of an object? 我应该从哪里开始在iPhone应用程序中制作行业标准的音频? - Where should I start in making industry standard audio in my iPhone applications? 我应该在哪里保存cocos2d Iphone游戏的gold / exp属性? - Where should I save gold/exp attributes of my cocos2d Iphone Game? 我可以将自己的应用程序放在我的iPhone上吗? - Can I put my own app on just my iphone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM