Home News Forum Social Networking Support Us Advertise

Spanish Lesson 1 ($1.99)

aWake!Gently ($1.99)

The Bird & The Snail - Knock Knock - Deluxe ($4.99)

Match-It Trains ($0.99)

Tangled ($0.99)

iFlatter ($0.99)

The 15 puzzle ($0.99)

Tap Forms Database ($8.99)

Higher or Lower Card Game (Hi Lo) ($0.99)

Red Pixel ($0.99)

Time-Shift Radio ($0.99)

Want your application advertised here? Only $10/week!

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-11-2009, 11:00 PM   #1 (permalink)
Junior Member
 
Join Date: Jan 2009
Posts: 8
Rep Power: 0
tadevil.com is on a distinguished road
Default NSArray and image

hi
i am new to iphone app dev.
i am trying to make a NSArray with couple of image in it. images are stored in app directory. i want to load all images and display images by accessing that array using index. so far i am using doing this to load images

array = [NSMutableArray arrayWithObjects: @"1.png", @"2.png", nil];

and accessing it like this way
UIImage *img= [self.array objectAtIndex:index];
[disimg setImage:[img CGImage] atIndex:index type:quality];

obviously i am doing something wrong, pls help
tadevil.com is offline   Reply With Quote
Old 01-12-2009, 12:13 AM   #2 (permalink)
Senior Member
 
RickMaddy's Avatar
 
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
Rep Power: 3
RickMaddy is on a distinguished road
Default

You're putting strings in the array, not images.
RickMaddy is offline   Reply With Quote
Old 01-12-2009, 10:26 AM   #3 (permalink)
Junior Member
 
Join Date: Jan 2009
Posts: 8
Rep Power: 0
tadevil.com is on a distinguished road
Default

that's what i was thinking too
i used it because i saw someone using the code this way
so can anyone guide me how to add images to an array, any online link would be great or just give an example
thank you
tadevil.com is offline   Reply With Quote
Old 01-12-2009, 11:11 AM   #4 (permalink)
Senior Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 999
Rep Power: 1
scotopia is on a distinguished road
Default

What I have found to work well is to load my images in an NSMutableArray using the addObject method with code as follows:

Code:
[myImageArray addObject:[UIImage imageWithContentsOfFile:[NSBundle mainBundle] pathForResource:@"image1" ofType:@"png"]];
Note that imageWithContentsOfFile does not cache the image in memory (which is generally better for most purposes to save memory).

I am using openGL now so I am actually adding Texture2Ds but the above code will work for regular images.
scotopia is offline   Reply With Quote
Old 01-12-2009, 11:21 AM   #5 (permalink)
Junior Member
 
Join Date: Jan 2009
Posts: 8
Rep Power: 0
tadevil.com is on a distinguished road
Default

i tried that one, but addobject only allows to add one image at a time (as far i know), so i have to write 10 line for 10 images.....
tadevil.com is offline   Reply With Quote
Old 01-12-2009, 11:25 AM   #6 (permalink)
Senior Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 999
Rep Power: 1
scotopia is on a distinguished road
Default

This is where we call on our good friend the for loop. If your images are named in a logical way (hatAnimation1.jpg, hatAnimation2.jpg etc) it is easy to set up a for loop to do this; I do this frequently.
scotopia is offline   Reply With Quote
Old 01-12-2009, 11:30 AM   #7 (permalink)
Junior Member
 
Join Date: Jan 2009
Posts: 8
Rep Power: 0
tadevil.com is on a distinguished road
Default

thanks for replying so quickly.
as i said before i am not an expert in programming
i am thinking of using loop but couldn't think of a way to use different filename in one loop. can you give me a hint please
thank you
tadevil.com is offline   Reply With Quote
Old 01-12-2009, 11:38 AM   #8 (permalink)
Senior Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 999
Rep Power: 1
scotopia is on a distinguished road
Default

You can find information on the docs on for loop pretty easily if you search around in "help" in xcode. I don't have my mac in front of me so I'll do the best I can from memory.

Basically it goes a little something like this:

Code:
int i;
NSString *loadString;

for(i = 0; i < 10; i++) {
loadString = [NSString stringWithFormat:@"hatAnimation%d", i]; 
[myImageArray addObject:[UIImage imageWithContentsOfFile:[NSBundle mainBundle] pathForResource:loadString ofType:@"png"]];

}
Now what this does will load in the images called hatAnimation1.png, hatAnimation2.png etc up to hatAnimation9.png.

Again, I am writing from memory here so my syntax might be slightly off but this is the jist; of course you must have your NSMutable Array allocated and released as usual in addition to the things above. Hope this helps!
scotopia is offline   Reply With Quote
Old 01-12-2009, 12:03 PM   #9 (permalink)
Junior Member
 
Join Date: Jan 2009
Posts: 8
Rep Power: 0
tadevil.com is on a distinguished road
Default

sorry for bothering u again
i implement the code you posted and now i am getting a warning
"UIImage may not respond to +imageWithContentsOf FIleathForResourcesf Type:"
any suggestion
tadevil.com is offline   Reply With Quote
Old 01-12-2009, 12:05 PM   #10 (permalink)
Senior Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 999
Rep Power: 1
scotopia is on a distinguished road
Default

I may have typoed. There definitely is a method like that for UIImage. Highlight the phrase UIImage in xCode and open research assistant; then click on "view class notes" or whatever it says and look through the class methods for one similar to what I wrote; it definitely exists.

I'm not sitting in front of my mac so I probably missed a bracket or typoed a word.

Last edited by scotopia; 01-12-2009 at 12:08 PM.
scotopia is offline   Reply With Quote
Old 01-12-2009, 12:20 PM   #11 (permalink)
Junior Member
 
Join Date: Jan 2009
Posts: 8
Rep Power: 0
tadevil.com is on a distinguished road
Default

i checked the reference and there's no typo in your code or mine.
one more question, what's the function of NSbundle here?
tadevil.com is offline   Reply With Quote
Old 01-12-2009, 12:23 PM   #12 (permalink)
Senior Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 999
Rep Power: 1
scotopia is on a distinguished road
Default

NSBundle is telling where to look for the file. The bundle is basically all the resources that are included with your app (images, sounds, files, etc).

If there are no typoes I don't know why it wouldn't work. Usually when you get errors like the one you listed it is because you are failing to include a header file or some such...but UIImage is a fairly essential class so it should be covered by the default headers when you start a new project; are you in some kind of custom object with this code or did you mess with the headers?

Last edited by scotopia; 01-12-2009 at 12:29 PM.
scotopia is offline   Reply With Quote
Old 01-12-2009, 12:43 PM   #13 (permalink)
Junior Member
 
Join Date: Jan 2009
Posts: 8
Rep Power: 0
tadevil.com is on a distinguished road
Default

I went for an inefficient way of using imagenamed
and i can now run the code and see the images but program crashed after 2-3 seconds...
[UIImage imageNamed:@......]
any thoughts
thanks
tadevil.com is offline   Reply With Quote
Old 01-12-2009, 12:58 PM   #14 (permalink)
Senior Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 999
Rep Power: 1
scotopia is on a distinguished road
Default

How many images are you loading and how large (pixel wise) are they?
scotopia is offline   Reply With Quote
Old 01-12-2009, 04:59 PM   #15 (permalink)
Senior Member
 
Join Date: Oct 2008
Posts: 429
Rep Power: 1
CommanderData is on a distinguished road
Default

Quote:
Originally Posted by scotopia View Post
I may have typoed. There definitely is a method like that for UIImage. Highlight the phrase UIImage in xCode and open research assistant; then click on "view class notes" or whatever it says and look through the class methods for one similar to what I wrote; it definitely exists.

I'm not sitting in front of my mac so I probably missed a bracket or typoed a word.
I think you were missing a bracket here and there (in case anyone else is playing along at home )

Code:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"]];
CommanderData is offline   Reply With Quote
Old 01-12-2009, 06:00 PM   #16 (permalink)
Senior Member
 
scotopia's Avatar
 
Join Date: Oct 2008
Posts: 999
Rep Power: 1
scotopia is on a distinguished road
Default

Thanks Commander, I suck on syntax when I don't have the nicey little spell checky stuff lol.
scotopia is offline   Reply With Quote
Old 03-18-2009, 03:51 PM   #17 (permalink)
Senior Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 289
Rep Power: 1
DenVog is on a distinguished road
Default

Quote:
Originally Posted by CommanderData View Post
I think you were missing a bracket here and there (in case anyone else is playing along at home )

Code:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"]];
I'm following along. Have tried this a couple different ways, but both present an error. The following gives me three "error: syntax error before ';' token" errors.
Code:
- (void)viewDidLoad {
	imageArray = [[NSMutableArray alloc] init];
	[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"rock" ofType:@"jpg"]];
	[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"paper" ofType:@"jpg"]];
	[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"scissors" ofType:@"jpg"]];
    [super viewDidLoad];
}
Doing it the way below runs, but gives me three "warning: 'NSArray' may not respond to '-addObject:'" warnings.

Code:
- (void)viewDidLoad {
	imageArray = [[NSMutableArray alloc] init];
	[imageArray addObject:[UIImage imageNamed:@"rock.jpg"]];
	[imageArray addObject:[UIImage imageNamed:@"paper.jpg"]];
	[imageArray addObject:[UIImage imageNamed:@"scissors.jpg"]];
    [super viewDidLoad];
}
If someone can tell me what I'm doing wrong, it would be appreciated. Thanks.

PS-I'm not building a Rock, Paper, Scissors application. It's just an example.
DenVog is offline   Reply With Quote
Old 06-25-2009, 02:19 PM   #18 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 2
Rep Power: 0
rusew is on a distinguished road
Default

Below, I think you are missing a bracket at the end of each line..

Code:
- (void)viewDidLoad {
	imageArray = [[NSMutableArray alloc] init];
	[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"rock" ofType:@"jpg"]];
	[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"paper" ofType:@"jpg"]];
	[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"scissors" ofType:@"jpg"]];
    [super viewDidLoad];
}
And here, I've had trouble with this method. The method above seems to work better. I'm not sure why, but, at least when there are a lot of images, the technique below crashes after a while.

Code:
- (void)viewDidLoad {
	imageArray = [[NSMutableArray alloc] init];
	[imageArray addObject:[UIImage imageNamed:@"rock.jpg"]];
	[imageArray addObject:[UIImage imageNamed:@"paper.jpg"]];
	[imageArray addObject:[UIImage imageNamed:@"scissors.jpg"]];
    [super viewDidLoad];
}
rusew is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


» Advertisements


» Stats
Members: 8,231
Threads: 20,199
Posts: 90,216
Top Poster: RickMaddy (2,121)
Welcome to our newest member, OneGlobe
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:36 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0