WoooHaaaa
WoooHaaaa

Reputation: 20440

Where should i put my images in my iPhone project?

I'm new to iPhone dev.

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 *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 ?


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];
}

Upvotes: 2

Views: 1123

Answers (4)

jay
jay

Reputation: 3667

You should create a folder named Resources and add images there.

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.

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

Upvotes: 1

Mat
Mat

Reputation: 7633

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. Moreover call self.imageView.image instead just imageView.image.

Upvotes: 0

Vipul
Vipul

Reputation: 28093

You have problem at this statement

[image release];

The easiest way to remember when to use release is NARC keyword :)

Always remember to make release call in 4 cases.

N New

A Alloc

R Retain

C Copy

Upvotes: 0

kulss
kulss

Reputation: 2055

there is no prbm with ur code. 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.

Upvotes: 0

Related Questions