iann iann - 1 year ago 96
iOS Question

How to pick a random image from NSArray and display randomly on view

Okay well I have made a couple threads asking how to display images randomly/pick a random image. What I have came to realize is that I do not know how to combine these two methods together.

I have 5 images in an array.

My .h file:

@property (strong, nonatomic) NSArray *imageArray;

My .m file:

- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

UIImage *image1 = [UIImage imageNamed:@"image-1.png"];
UIImage *image2 = [UIImage imageNamed:@"image-2.png"];
UIImage *image3 = [UIImage imageNamed:@"image-3.png"];
UIImage *image4 = [UIImage imageNamed:@"image-4.png"];
UIImage *image5 = [UIImage imageNamed:@"image-5.png"];

_imageArray = @[image1, image2, image3, image4, image5];

This is what I have right now, I have been playing around with other code ive found elsewhere but have had no success so left it out.

Now that you see what I have, this is what im trying to do:
I need to use a method that will pick one of my 5 images randomly from my array and then display it in a random position in my view.

I also need to repeat this loop, but with constraints. I need each image to have a "value" such as: image-1 equal to 1, image-2 equal to 2, image-3 equal to 3, image-4 equal to 4, and image-5 equal to 5. And have the loop repeat until the images displayed equals a total value of 50.

Im not really sure where to begin on what methods to use. Im sure picking and displaying randomly is easy to some of you but the values and repeating until equaling 50 seems complex. So any and all help is greatly appreciated! Thanks in advance to anyone who can help, im new to coding so if you can explain why you used your code it would help even more! Thanks!

Edit: This guy tried helping me in my other thread adding the whole picking a random image as well. His response is here: How to display multiple UIImageViews and I used his code but nothing happened. I am not sure if his code is wrong somewhere or if I did something wrong.

Answer Source

Use a recursive loop and an int to keep track of your progress towards your desired count of 50.

Each time through the loop, you'll want to:

  1. Generate a new random number (0-4)
  2. Use your random number to select an image from your array
  3. Add your random number to your int and check to see if you've hit 50.
  4. If you've hit 50, you're done
  5. If you haven't hit 50, do it again.

Something like this:

//in your .h declare an int to track your progress
int myImgCount;

//in your .m
-(void)randomizeImages {

  //get random number
  int randomImgNum = arc4random_uniform(5);  

  //use your random number to get an image from your array
  UIImage *tempImg = [_imageArray objeactAtIndex:randomImgNum];

  //add your UIImage to a UIImageView and place it on screen somewhere
  UIImageView *tempImgView = [[UIImageView alloc] initWithImage:tempImg];  

  //define the center points you want to use
  tempImgView.center = CGPointMake(yourDesiredX,yourDesiredY);   

  [self addSubview:tempImgView];
  [tempImgView release];

  //increment your count
  myImgCount = myImgCount+(randomImgNum+1); 

  //check your count
  if (myImgCount<50) {
    [self randomizeImages];  //do it again if not yet at 50


Something like that should work for you.