iphone - Getting Images From Array Change Randomly -


i developing 1 app in app images nsarray , store imageview image view scroll vertically , each every page has single image.

i want display random images when app start each every time.all images shuffle every time.

i tried one.

    uiscrollview *scrollview=[[uiscrollview alloc]initwithframe:cgrectmake(0, 0, 320, 490)];       [scrollview setpagingenabled:yes];       [scrollview setshowshorizontalscrollindicator:no];       nsarray *imagearray=[[nsarray alloc]initwithobjects:@"image1",@"image2",@"image3",@"image4",@"image5", nil];      for( i=0; i< [imagearray count];i++)     {          int i=arc4random()%[imagearray count];   // shuffle images          nsstring *imagename=[imagearray objectatindex:i];           nsstring *fullimagename=[nsstring stringwithformat:@"%@.jpeg",imagename];           int padding=25;          cgrect imageviewframe=cgrectmake(scrollview.frame.size.width*i+padding, scrollview.frame.origin.y, scrollview.frame.size.width-2*padding, scrollview.frame.size.height);           uiimageview *imageview=[[uiimageview alloc]initwithframe:imageviewframe];          [imageview setimage:[uiimage imagenamed:fullimagename]];           [imageview setcontentmode:uiviewcontentmodescaleaspectfit];          [scrollview addsubview:imageview];      }      cgsize scrollviewsize=cgsizemake(scrollview.frame.size.width*[imagearray count], scrollview.frame.size.height);       [scrollview setcontentsize:scrollviewsize];      [self.view addsubview:scrollview]; 

images displayed not change randomly. me out in advance.

your algorithm has serious flaw in don't ensure haven't chosen same index twice. also, sake of writing code properly, replace "i" else:

int randidx=arc4random()%[imagearray count];   // shuffle images nsstring *imagename=[imagearray objectatindex:randidx]; 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -