Thursday, 7 June 2012

How to make image gallary on scrollview in iphone sdk

- (void)setupDisplayFiltering{
   
    NSArray *subview = [[mainScrollView subviews] copy];
    for (UIView *subview1 in subview) {
        [subview1 removeFromSuperview];
    }
    [subview release];
   
    mainScrollView.delegate = self;
   
    [mainScrollView setBackgroundColor:[UIColor clearColor]];
    [mainScrollView setCanCancelContentTouches:NO];
   
    mainScrollView.clipsToBounds = YES;
    mainScrollView.scrollEnabled = YES;
    mainScrollView.pagingEnabled = YES;
   
      
    int x;
    int y,h,w;
    x=15,y=0,h=135,w=135;
    //int i = array.count;
    int i = CategoryArray.count;
    for (j=0; j<i; j++) {
        if (j==0) {
            x=15;
            y=25;
        }
        else{  
            if (j%2==0) {
                x=15;
                y=y+20+135;
                mainScrollView.contentSize = CGSizeMake(242, 416+y-80);
            }
            else{
                x=x+135+20;
            }
        }
        NSString *image = [[CategoryArray objectAtIndex:j] valueForKey:@"i_image"];
       
        AsyncImageView* asyncImage = [[AsyncImageView alloc] initWithFrame:CGRectMake(0, 0, 135, 135)];
        asyncImage.tag = 999;
       
        NSURL* url = [NSURL URLWithString:image];
        asyncImage.userInteractionEnabled = NO;
        [asyncImage loadImageFromURL:url];

        btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnImage addTarget:self action:@selector(btnImageClicked:) forControlEvents:UIControlEventTouchUpInside];
        btnImage.frame = CGRectMake(x, y, h, w);
       
        [mainScrollView addSubview:btnImage];
        [btnImage addSubview:asyncImage];

        btnImage.tag = j;
       
        NSLog(@"%d",btnImage.tag);
       
        UILabel *lblFirst = [[UILabel alloc]init] ;
        lblFirst.frame = CGRectMake(22, 135, 140, 18);
        //lblFirst.font=[UIFont boldSystemFontOfSize:16];
        lblFirst.font=[UIFont systemFontOfSize:13];
        lblFirst.backgroundColor = [UIColor clearColor];
        lblFirst.textColor = [UIColor blackColor];
        [lblFirst setText:[[CategoryArray objectAtIndex:j]valueForKey:@"i_name"]];
        [btnImage addSubview:lblFirst];


    }
}

No comments:

Post a Comment