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-04-2009, 12:07 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 22
Default Randomize picture

hello i am making an app that would involve randomizing a picture, but im not sure what to do.
I wanted it to be able to click anywhere on the screen for another random image.

If anyone knows the code plzzz leave a reply thank you.
dsinc14 is offline   Reply With Quote
Old 07-04-2009, 03:40 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2008
Posts: 459
Default

Quote:
Originally Posted by dsinc14 View Post
hello i am making an app that would involve randomizing a picture, but im not sure what to do.
I wanted it to be able to click anywhere on the screen for another random image.

If anyone knows the code plzzz leave a reply thank you.
Well, let me try to put together a basic workflow for your problem:

1) Create an array and add names of your images to it.
2) Add a UIImageView element with the size of your screen to the view.
3) Implement a method to generate a random number, like:
Code:
- (int)getRandomNumber:(int)from to:(int)to {
	
	return (int)from + arc4random() % (to-from+1);
}
3) Implement the touches ended method, like:

Code:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
        //detect the touch
        if (CGRectContainsPoint([nameOfYourUIImageView frame], [touch locationInView:self.view])) {
              //set the new random image to your UIImageView
              nameOfYourUIImageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",[nameOfYourArray objectAtIndex:[self getRandomNumber:0 to:[nameOfYourArray count]-1]]]  ofType:@"png"]];
             //right above we're calling the getRandomNumber method, we previously implemented. As expected it gets us a random number between 0 (arrays start at 0) and the maximum items in that array. We're also subtracting 1 from the array count (max items in the array), in order to prevent the app from crashing. To make that clear: arrays start from 0, but if you call [nameOfYourArray count] it gets you the number of items in your array and of course starts counting at 1.
}
I hope that gives you a basic idea and helps you to get started. Just browse this forum for more detailed examples on each of the tasks I described. Good luck!
NewiPhoneDeveloper is offline   Reply With Quote
Old 07-04-2009, 04:21 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 22
Thumbs up Ty

Thank you for your help. It really helped.
dsinc14 is offline   Reply With Quote
Old 07-04-2009, 05:42 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 22
Default Fill In?

What do i need to fill in with my data.



- (int)getRandomNumber:(int)from to:(int)to {

return (int)from + arc4random() % (to-from+1);
}

Last edited by dsinc14; 07-04-2009 at 05:44 PM.
dsinc14 is offline   Reply With Quote
Old 07-04-2009, 05:51 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2008
Posts: 459
Default

Quote:
Originally Posted by dsinc14 View Post
What do i need to fill in with my data.



- (int)getRandomNumberint)from toint)to {

return (int)from + arc4random() % (to-from+1);
}
I usually create a NSMutableArray in my .h file, like:

Code:
NSMutableArray *nameOfMyArray;

//below } of @interface in your .h file @property:

@property (nonatomic, retain) NSMutableArray *nameOfMyArray;
in your .m, first synthesize:

Code:
@synthesize nameOfMyArray;

//if you have a viewDidLoad method, allocate space for your array there and add the items:

nameOfMyArray = [[NSMutableArray alloc] initWithObjects:@"nameOfImage1Without_without.png", @"nameOfImage2Without.png", nil];

//don't forget to release your array inside the dealloc method:

- (void)dealloc {
    [nameOfMyArray release];
    [super dealloc];
}
NewiPhoneDeveloper 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: 157,867
Threads: 88,917
Posts: 379,300
Top Poster: BrianSlick (7,072)
Welcome to our newest member, Ranahingts
Powered by vBadvanced CMPS v3.1.0

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