0

LOGIC:

  • 5 image in a array

example:

apple.png,mango.png,bird.png,sun.png,moon.png

I need to use arc4random in this array, so I got index as 1, so value got mango.png

in other array I stored

number = [[NSMutableArray alloc]initWithObjects:@"4",@"2",@"9",@"1",@"8",@"7",@"5",@"3",@"6",@"10", nil];

and used arc4random and i got the output as index:2 value as:9

then i need to print 9 times image of mango in image view.

please help how to code this using objective-c

6
  • Where do you want to display them? Commented Sep 1, 2017 at 5:55
  • i need to display in output by using nslog Commented Sep 1, 2017 at 5:58
  • means you need to display image 3 times in nslog ? Commented Sep 1, 2017 at 6:00
  • What do you want to log with NSLog? The UIImage object description? Commented Sep 1, 2017 at 6:00
  • NSLog(@"%@",someVariablecontainingaUIImageReference) ? Commented Sep 1, 2017 at 6:03

3 Answers 3

1

As per my understanding, you have to show random Image in ImageView on the basis of random number.

Have a look below code,

images = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"],[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"], nil];

int randomIndex=arc4random() % images.count;
UIImage *selectedImage = [images objectAtIndex:randomIndex]; //random selected image

number = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];
int randomNumber = arc4random() % number.count; //random selected number

NSInteger indexValue = [number indexOfObject:[NSString stringWithFormat:@"%d",randomNumber]]; // get index number of your random number

for (int i = 0; i <indexValue ; i++) {
    NSLog(@"%@", selectedImage);
    yourImageView.Image = selectedImage;
}
Sign up to request clarification or add additional context in comments.

10 Comments

i got the answer but , in number array have 10 number and random number =2
Yes, your number arrays has 10 numbers and random number 2 then for loop will run 2 times and image will show two time. Do want to know else?
i need the display the value stored in the index not the random number
I didn't get your question. I really don't understand which index you are talking about. Please explain more
i got randomIndex =1 then selectedImage =Christmas-Wallpapers-HD-Picture.jpg so i used to display that image, such way i need the answer as random number=2 then selectednumber=3 .how to do
|
0

you have to show number of times images in NSLog as per the random number comes. so just add the following code at the end of your existing code.

int count = [[number objectAtIndex:ran] intValue];
    for(int i=0; i<count; i++)
    {
        NSLog(@"%@",v);
    }

1 Comment

@jesteena just add this 5 line code of your existing code (at the end) and check it
0

Important thing is where do you want to display the image.

From the above snippet what I can understand is v(NSArray) is assigned with UIImage. If you want to use v as NSArray then add UIImage as an object to the Array, then you will be able to access image using index.

NSArray *v=[[NSArray alloc] initWithObjects:[imagesArray objectAtIndex:randomIndex], nil];

then you can NSLog the image object with below code

for (int i= 0 ; i<ran; i++) {
    NSLog(@"%@",[v objectAtIndex:0]);
   _cardsImageView.image = [v objectAtIndex:0]
}

2 Comments

i need display in imageview.
you need to create UIImageview to display the content. If Imageview is setup then you can display using below code UIImage _cardsImageView.image = [v objectAtIndex:0];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.