Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

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

Reply
 
LinkBack Thread Tools Display Modes
Old 07-08-2009, 12:59 PM   #1 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 10
Exclamation Select Random Picture Within Array

Hello,
I am pretty new to programming on iphone and have gotten stuck with something. I am trying to select a random picture from my array. Also, I only know how to make my array show as an animation and i was wondering how to make it so it just shows the image in the image view instead of animating it. I plan on having 300-500 images total. Thanks for any help! My code is attached.
Attached Files
File Type: zip Untitled.zip (931 Bytes, 68 views)
Samalope is offline   Reply With Quote
Old 07-08-2009, 01:58 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by Samalope View Post
Hello,
I am pretty new to programming on iphone and have gotten stuck with something. I am trying to select a random picture from my array. Also, I only know how to make my array show as an animation and i was wondering how to make it so it just shows the image in the image view instead of animating it. I plan on having 300-500 images total. Thanks for any help! My code is attached.
I did not look at your code, but It sounds like you're using a UIImageView and setting the animationImages property. If you set myView.animationImages = nil then you can set myView.image = anImage. That will display the single image "anImage".

300 images is a lot, probably too many to load into memory at the same time. Instead, you should probably keep an NSArray of image *filenames* , and when you pick your random number, take that filename and load the image.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-08-2009, 02:22 PM   #3 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 10
Smile Select Random Picture Within Array

Quote:
Originally Posted by smasher View Post
I did not look at your code, but It sounds like you're using a UIImageView and setting the animationImages property. If you set myView.animationImages = nil then you can set myView.image = anImage. That will display the single image "anImage".

300 images is a lot, probably too many to load into memory at the same time. Instead, you should probably keep an NSArray of image *filenames* , and when you pick your random number, take that filename and load the image.


could you give me an example of what an NSArray of image *filenames* would look like? And would I use the arc4random() function with that?


I really appreciate your help! Thank You!
Samalope is offline   Reply With Quote
Old 07-08-2009, 02:44 PM   #4 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default

Yes,

You can do something like:

Code:
NSInteger *picToSelect = arc4random() % PICTURE_COUNT;
return [pictureArray objectAtindex:picToSelect];
or:

Code:
NSString *imageName = [pictureArray objectAtIndex:[arc4random() % PICTURE_COUNT]];
Where PICTURE_COUNT is the total # of images you have, and pictureArray is an NSArray(or muteable, etc) that stores all the image names.

That's just off the top of my head, so could be incorrect, but should help.

- sk
mr-sk is offline   Reply With Quote
Old 07-08-2009, 02:52 PM   #5 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by Samalope View Post
could you give me an example of what an NSArray of image *filenames* would look like? And would I use the arc4random() function with that?

I really appreciate your help! Thank You!
Something like this -

Code:
//create an array of filenames
NSMutableArray *fileNames = [[NSMutableArray alloc ] initWithObjects: @"one", @"a", @"two", @"b", @"three", @"four", nil];

//pick one filename
int numFileNames = [fileNames count];
int chosen = arc4random() % numFileNames;
NSString *oneFilename = [fileNames objectAtIndex: chosen];

//load your image here
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-08-2009, 02:53 PM   #6 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 10
Default

Quote:
Originally Posted by mr-sk View Post
Yes,

You can do something like:

Code:
NSInteger *picToSelect = arc4random() % PICTURE_COUNT;
return [pictureArray objectAtindex:picToSelect];
or:

Code:
NSString *imageName = [pictureArray objectAtIndex:[arc4random() % PICTURE_COUNT]];
Where PICTURE_COUNT is the total # of images you have, and pictureArray is an NSArray(or muteable, etc) that stores all the image names.

That's just off the top of my head, so could be incorrect, but should help.

- sk
Ok but do i still need to make the array like this:

[NSArray arrayWithObjects:
[UIImage imageNamed: @"name1.png"],
[UIImage imageNamed: @"name2.png"],
[UIImage imageNamed: @"name3.png"],
[UIImage imageNamed: @"name4.png"],
[UIImage imageNamed: @"name5.png"],
[UIImage imageNamed: @"name6.png"],
[UIImage imageNamed: @"name7.png"],

[UIImage imageNamed: @"name8.png"], nil];



And do that all the way up to 300? or would it just pull that from my resources folder? Thanks
Samalope is offline   Reply With Quote
Old 07-08-2009, 03:07 PM   #7 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default

Hey, you could do like:

Code:
for (int c=0;c<300;c++)
{
    NSString *imageName = [NSString stringWithFormat:@"name%d.png", c];
    [UIImage imageNamed: imageName];
}
Something like that?
mr-sk is offline   Reply With Quote
Old 07-08-2009, 03:16 PM   #8 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 10
Default Mr sk thanks...

Quote:
Originally Posted by mr-sk View Post
Hey, you could do like:

Code:
for (int c=0;c<300;c++)
{
    NSString *imageName = [NSString stringWithFormat:@"name%d.png", c];
    [UIImage imageNamed: imageName];
}
Something like that?


Hi Mr Sk, that seems to be what i want to do. It does not do anything when i run it, I get the error:

"Terminating due to uncaught exception"

Do I need anything before or after that chunk of code you gave me?
Samalope is offline   Reply With Quote
Old 07-08-2009, 03:22 PM   #9 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default

heh sorry - try this:

Code:
NSMutableArray *imageArray;
for (int c=0;c<300;c++)
{
    NSString *imageName = [NSString stringWithFormat:@"name%d.png", c];
    [imageArray addObject: imageName];
}
Something like that. It should add all the objects to "imageArray".
Set a break point and make sure its working.
mr-sk is offline   Reply With Quote
Old 07-08-2009, 09:25 PM   #10 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 10
Smile Thanks :)

Quote:
Originally Posted by mr-sk View Post
heh sorry - try this:

Code:
NSMutableArray *imageArray;
for (int c=0;c<300;c++)
{
    NSString *imageName = [NSString stringWithFormat:@"name%d.png", c];
    [imageArray addObject: imageName];
}
Something like that. It should add all the objects to "imageArray".
Set a break point and make sure its working.
Hey thanks for the all the info you gave us, it helped a lot. we did it a little bit differently than how you said but the info you gave us contributed to our success. We have a new problem though: the loaded image loads to the top left of the screen. I have another post with the specific info so if you know anything that could be going wrong, please tell us. Thanks again! The link is:

http://www.iphonedevsdk.com/forum/ip...rong-spot.html
Samalope is offline   Reply With Quote
Old 07-08-2009, 10:40 PM   #11 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default

Coolio man - glad we could help.

I'll take a peak at your other thread.
mr-sk is offline   Reply With Quote
Old 07-15-2009, 03:11 PM   #12 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 23
Default

I been trying to accomplish something along the same lines but I only have about 10 images. I also want to have a options menu where I can turn groups of images off. So if the flick the switch of mountians off then no mountains will be generated. Since I only have about 10 image should I go with some more like this:
Code:
NSString *imagePath = [NSString stringWithFormat:@"image%d.png", (arc4random() % 10) + 1];
instead of a array
noobforlife is offline   Reply With Quote
Reply

Bookmarks

Tags
array, image, random, view

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
» Online Users: 351
18 members and 333 guests
ADY, Dani77, Duncan C, e2applets, Grinarn, HemiMG, Herbie, JasonR, linkmx, macquitzon216, mer10, Monstertaco, prchn4christ, Robiwan, sly24, Touchmint, twerner, zulfishah
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,881
Threads: 89,228
Posts: 380,760
Top Poster: BrianSlick (7,129)
Welcome to our newest member, macquitzon216
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:39 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0