1

In my FirstViewcontroller I have picker containing 1 to 25 values, if the user select 5 in the picker on the next screen it should display 5 images randomly out of 25 images without repetition of that 5 images. If user selects another number means it takes that number as input and it should display images according to that number. If I run my code means every time it showing all 25 images not selected number images. Here is my code

- (void)viewDidLoad {    
    myImageNames = [[NSMutableArray alloc] init];

    [myImageNames addObject:[UIImage imageNamed:@"Red.png"]];
    [myImageNames addObject:[UIImage imageNamed:@"Blue.png"]];
    [myImageNames addObject:[UIImage imageNamed:@"LightRed.png"]];
    [myImageNames addObject:[UIImage imageNamed:@"Orange.png"]];
    .
    .
    .

    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self  selector:@selector(ChangingImgView:) userInfo:nil repeats:YES];
}

- (void) ChangingImgView:(int)selectedNumberFromPicker {
    selectedNumberFromPicker = [num intValue];

    for (int i=0; i < selectedNumberFromPicker; i++) {

        index = arc4random() % 25;
        NSString *strIndex = [NSString stringWithFormat:@"%i",index];
        [myImageNames addObject:strIndex];
        imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        imgBtn.frame = CGRectMake(250, 300, 170, 220);
        UIImage *img = [myImageNames objectAtIndex:index];
        [imgBtn setBackgroundImage:img forState:UIControlStateNormal];
        [imgBtn addTarget:self action:@selector(ClickingOnImage) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:imgBtn];
    }
}

Please help me.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147

4 Answers4

1

you can also use NSSet instead of using NSMutableArray

doc apple

You can use sets as an alternative to arrays when the order of elements isn’t important and performance in testing whether an object is contained in the set is a consideration—while arrays are ordered, testing for membership is slower than with sets.

iArezki
  • 1,273
  • 2
  • 17
  • 29
0

Firstly suffle your myImageNames which has images using this link.

Now display first 5 images from myImageNames.

Community
  • 1
  • 1
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0
int count = 5;
NSMutableArray *inputImages = [myImageNames mutableCopy];
NSMutableArray *randomImages;

for (int i = 0; i<count; i++) {
    int index = arc4random_uniform([inputImages count]);
    [randomImages addObject:[inputImages objectAtIndex:index]];
    [inputImages removeObjectAtIndex:index];
}
Jonathan Cichon
  • 4,396
  • 16
  • 19
0

Try using this for loop to get the particular number of images for random indexes. Use selectedImgArray to display the selected images. myImageNames is your original array.

NSInteger index;
NSMutableArray *selectedImgArray = [[NSMutableArray alloc] init];
for (int i=0; i < selectedNumberFromPicker; i++) {
    index = arc4random() % 25;
    UIImage *selImg = (UIImage *)[myImageNames objectAtIndex:index];
    if ([selectedImgArray containsObject:selImg]) {
        i--;
        continue;
    }
    [selectedImgArray addObject:selImg];

}
iDev
  • 11
  • 2