如何在iphone中的gridview或imagegallery中显示照片 [英] How to display photos in gridview or imagegallery in iphone

查看:144
本文介绍了如何在iphone中的gridview或imagegallery中显示照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在tableview自定义单元格中有不同数量的照片。现在我想在下一页显示照片
。每行有4张照片。我可以这样做吗?

UIScrollView 来做这件事...



您网格中的每个项目都是 UIImageView ,您将它们作为子视图添加到 UIScrollView 中。

下面的代码可以嵌入视图控制器中的 viewDidLoad 中,并假定你已经在界面中设置了你的UISCrollView builder,否则你需要在 viewDidLoad 内分配滚动视图。



代码会将可变数量的图像项添加到具有4列的UIScrollView中。

  UIImageView * imageView = nil; 

//菜单项的x和y视图位置坐标
int x = 0,y = 0;

//你想要的图片数量
int numOfItemsToAdd = 10;

//图像的高度和宽度是屏幕宽度除以列数
int imageHeight = 320/4,imageWidth = 320/4;

int numberOfColumns = 4;

//内容抓取需要重申将添加的项目数
[scrollView setContentSize:CGSizeMake(320,imageHeight * numOfItemsToAdd];
for(int i = 0; i< numOfItemsToAdd; i ++){
if(i%numberOfColumns == 0){//%你想要的列数
x = 0;
if(i!= 0)
y + = imageHeight;
} else {
x = imageHeight;
}
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,imageWidth,imageHeight) ];

//在scrollviews坐标系中设置图像的中心
imageView.center = CGPointMake(x,y);

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

// Finaly将图像添加到scorll视图
[scrollView addSubview:imageView];


I have different number of photos in tableview custom cell.Now i want to display that photos 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];

这篇关于如何在iphone中的gridview或imagegallery中显示照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆