Sunday, October 21, 2012

create image grid on iOS



  - (void)viewDidLoad
{  

   myScroolView=[[scrollView alloc] init];
  [myScroolView setFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.x, self.view.frame.size.width , self.view.frame.size.height)];
   myScroolView.backgroundColor = [UIColor clearColor];
   myScroolView.userInteractionEnabled=YES;
   myScroolView.delegate=self;
    
    activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicator.frame = CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/2, 30, 30);
    [self.view addSubview:activityIndicator];
    activityIndicator.hidden = YES;
    
       // Do any additional setup after loading the view from its nib.
[self createGrid];
}



-(void)createGrid
{
    NSLog(@"createGrid");
   
        [self.view addSubview:myScroolView];
        
       
        int outer=[YourImageArray count]/4;
        int inner=4;
   
        outer=([YourImageArray count]/4 -outer)>0? outer :outer+1;
        [myScroolView setContentSize:CGSizeMake(1024.0f, (outer*80)+30.0f)];
        for (int i=0,t=0;i<outer;i++)
        {
            if (i == outer-1) {
                inner=[YourImageArray count]%4;
                
            }
            for (int j=0; j<inner; j++) {
                
                UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(j*231.0f+90.0f, i*231.0f+90.0f,231.0f,231.0f)];
                myImage.image = [UIImage imageWithContentsOfFile:[YourImageArray objectAtIndex:t]];
                myImage.tag = t;
                [myImage setUserInteractionEnabled:YES];
                [myScroolView addSubview:myImage];
                [myImage release];
                t++;
            }
             
        }
        
        
    [activityIndicator stopAnimating];

    
}

No comments:

Post a Comment