开发者

How to display photos in gridview or imagegallery in iphone

I have different number of photos in tableview custom cell.Now i want to display that pho开发者_StackOverflow中文版tos on next page.4 photos per line.how can i do that?


You can use a UIScrollView for this...

Each item in your grid is a UIImageView which you add as subviews to your UIScrollView.

The following code can be insterted in viewDidLoad inside your view controller and assumes that you have setup your UISCrollView in interface builder, otherwise you would need to allocate your scroll view inside viewDidLoad.

The code will add a variable number of image items to a UIScrollView with 4 columns.

UIImageView *imageView = nil;

//The x and y view location coordinates for your menu items 
int x = 0, y = 0;

//The number of images you want 
int numOfItemsToAdd = 10; 

//The height and width of your images are the screen width devided by the number of columns 
int imageHeight = 320/4, imageWidth = 320/4; 

int numberOfColumns = 4; 

//The content seize needs to refelect the number of items that will be added
[scrollView setContentSize:CGSizeMake(320, imageHeight*numOfItemsToAdd];
for(int i=0; i<numOfItemsToAdd; i++){
    if(i%numberOfColumns == 0){//% the number of columns you want
        x = 0;
        if(i!=0)
            y += imageHeight;  
    }else{
        x = imageHeight;  
    }
    imageView = [[UIImageView alloc] initWithFrame: CGRectMake(x, y, imageWidth, imageHeight)];

    //set the center of the image in the scrollviews coordinate system 
    imageView.center = CGPointMake(x, y); 

    imageView.image = [UIImage imageNamed:@"my_photo.png"]; 

    //Finaly add the image to the scorll view
    [scrollView addSubview:imageView];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜