 |
 |
|
 |
07-03-2009, 11:07 PM
|
#1 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 22
|
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.
|
|
|
07-04-2009, 02:40 PM
|
#2 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Posts: 401
|
Quote:
Originally Posted by dsinc14
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!
|
|
|
07-04-2009, 03:21 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 22
|
Ty
Thank you for your help. It really helped.
|
|
|
07-04-2009, 04:42 PM
|
#4 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 22
|
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 04:44 PM.
|
|
|
07-04-2009, 04:51 PM
|
#5 (permalink)
|
|
Registered Member
Join Date: Jul 2008
Posts: 401
|
Quote:
Originally Posted by dsinc14
What do i need to fill in with my data.
- (int)getRandomNumber  int)from to  int)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];
}
|
|
|
 |
| 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: 384 |
| 51 members and 333 guests |
| BolderImage, ChrisMayer, damienbern, dave1619, daxappvi, Don, dre, drunknbass, ershaer, Exaviorn, firearasi, Forsworn, frankenspank, funhog, gonk, gshaviv, harrytheshark, hatembr, headkaze, hm50, iGeorG, illinnovations, imhotep85, Janek2004, JonnyBGoode, jyu, Kalimba, kiksy, KinAz, listingboat, Locker, MadMark, masc2279, moehac, mpramodjain, Mr Jack, nobre84, Noise, PhotoShootoutApp, refreshe, Rudy, rybek, SalvoMaltese, sayer, sfeast, SimonHodgkiss, stifmaister, TunaNugget, upperhouse, wilky94, zulfishah |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,203
Threads: 38,982
Posts: 171,002
Top Poster: smasher (2,568)
|
| Welcome to our newest member, sfeast |
|