 |
 |
|
 |
01-11-2009, 11:00 PM
|
#1 (permalink)
|
|
Junior Member
Join Date: Jan 2009
Posts: 8
Rep Power: 0
|
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
|
|
|
01-12-2009, 12:13 AM
|
#2 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Location: Denver, CO
Posts: 2,121
Rep Power: 3
|
You're putting strings in the array, not images.
|
|
|
01-12-2009, 10:26 AM
|
#3 (permalink)
|
|
Junior Member
Join Date: Jan 2009
Posts: 8
Rep Power: 0
|
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
|
|
|
01-12-2009, 11:11 AM
|
#4 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Posts: 999
Rep Power: 1
|
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.
|
|
|
01-12-2009, 11:21 AM
|
#5 (permalink)
|
|
Junior Member
Join Date: Jan 2009
Posts: 8
Rep Power: 0
|
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.....
|
|
|
01-12-2009, 11:25 AM
|
#6 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Posts: 999
Rep Power: 1
|
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.
|
|
|
01-12-2009, 11:30 AM
|
#7 (permalink)
|
|
Junior Member
Join Date: Jan 2009
Posts: 8
Rep Power: 0
|
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
|
|
|
01-12-2009, 11:38 AM
|
#8 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Posts: 999
Rep Power: 1
|
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!
|
|
|
01-12-2009, 12:03 PM
|
#9 (permalink)
|
|
Junior Member
Join Date: Jan 2009
Posts: 8
Rep Power: 0
|
sorry for bothering u again
i implement the code you posted and now i am getting a warning
"UIImage may not respond to +imageWithContentsOf FIle  athForResources  f Type:"
any suggestion
|
|
|
01-12-2009, 12:05 PM
|
#10 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Posts: 999
Rep Power: 1
|
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.
|
|
|
01-12-2009, 12:20 PM
|
#11 (permalink)
|
|
Junior Member
Join Date: Jan 2009
Posts: 8
Rep Power: 0
|
i checked the reference and there's no typo in your code or mine.
one more question, what's the function of NSbundle here?
|
|
|
01-12-2009, 12:23 PM
|
#12 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Posts: 999
Rep Power: 1
|
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.
|
|
|
01-12-2009, 12:43 PM
|
#13 (permalink)
|
|
Junior Member
Join Date: Jan 2009
Posts: 8
Rep Power: 0
|
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
|
|
|
01-12-2009, 12:58 PM
|
#14 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Posts: 999
Rep Power: 1
|
How many images are you loading and how large (pixel wise) are they?
|
|
|
01-12-2009, 04:59 PM
|
#15 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Posts: 429
Rep Power: 1
|
Quote:
Originally Posted by scotopia
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"]];
|
|
|
01-12-2009, 06:00 PM
|
#16 (permalink)
|
|
Senior Member
Join Date: Oct 2008
Posts: 999
Rep Power: 1
|
Thanks Commander, I suck on syntax when I don't have the nicey little spell checky stuff lol.
|
|
|
03-18-2009, 03:51 PM
|
#17 (permalink)
|
|
Senior Member
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 289
Rep Power: 1
|
Quote:
Originally Posted by CommanderData
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.
|
|
|
06-25-2009, 02:19 PM
|
#18 (permalink)
|
|
New Member
Join Date: Jun 2009
Posts: 2
Rep Power: 0
|
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];
}
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
|
» Online Users: 235 |
| 29 members and 206 guests |
| 2ndSegment, bondjpf, crossfire, dapis, davidlansalot, ElysianEagle, enfamus, exosyphen, freshking, g.castaldi, gbh, Groucho, hugo, joshsroka, Jume, Lars, lastiko, lildragon, martinws, mattjgalloway, MetaImi, mnemonic_fx, nattylux, OneGlobe, Opticfibre, shabzcohelp, Sicga, StatCoder, williamlegate |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 8,231
Threads: 20,199
Posts: 90,216
Top Poster: RickMaddy (2,121)
|
| Welcome to our newest member, OneGlobe |
|