iOS动态布局图像

iOS动态布局图像,ios,layout,Ios,Layout,我试图为iOS中的布局问题找到一个合理的解决方案 我需要在具有此布局的视图中加载n个图像: ----------- | x x | | x x | | x x | | x x | | x x | | x x | ----------- 每个图像可能有不同的宽度,但高度有一个最大值,并且每个图像都要在其列中居中 建议的方法是什么 我正在考虑将50%宽度的视图并排放置,然后将图像居中放置 谢谢 看起来与文本引擎在渲染glyph时遇到的问题相同。您要做

我试图为iOS中的布局问题找到一个合理的解决方案

我需要在具有此布局的视图中加载n个图像:

-----------
|  x   x  |
|  x   x  |
|  x   x  |
|  x   x  |
|  x   x  |
|  x   x  |
-----------
每个图像可能有不同的宽度,但高度有一个最大值,并且每个图像都要在其列中居中

建议的方法是什么

我正在考虑将50%宽度的视图并排放置,然后将图像居中放置


谢谢

看起来与文本引擎在渲染glyph时遇到的问题相同。您要做的是先计算行的宽度,然后再执行其他操作。这是通过从左到右循环来完成的,添加视图的大小,直到到达边缘,不能再添加更多。然后知道该行的宽度和最大高度,只需向左侧添加一个偏移量,该偏移量等于从左向右除以二的边距,即可使视图居中。然后按该行的最大高度向y坐标添加偏移


然后,只需对所有视图执行此操作,直到它们都有一个位置。

看起来与文本引擎在渲染图示符时遇到的问题相同。您要做的是先计算行的宽度,然后再执行其他操作。这是通过从左到右循环来完成的,添加视图的大小,直到到达边缘,不能再添加更多。然后知道该行的宽度和最大高度,只需向左侧添加一个偏移量,该偏移量等于从左向右除以二的边距,即可使视图居中。然后按该行的最大高度向y坐标添加偏移


然后对所有视图继续这样做,直到它们都有一个位置。

这应该可以让您开始。然而,它没有考虑图像的比率

CGRect  bounds = [[UIScreen mainScreen] bounds];
CGFloat maxCol = [imageViewArray count] + ([imageViewArray count] % 2);
CGFloat width  = (bounds.size.width/2) - 15;
CGFloat height = (bounds.size.height/maxCol) - 10; 

// loop through imageViews
for(pos = 0; pos < [imageViewArray count]; pos++)
{
   // calculate position within grid
   CGFloat row = (pos/2);
   CGFloat col = (pos%2);


   // retrieves UIImageView and UIImage
   imageView       = [imageViewArray objectAtIndex:pos];
   image           = imageView.image;

   // calculates image size
   CGFloat scaledWidth = (height * image.size.width) / image.size.height;

   // adjust size of image view
   imageView.frame = CGRectMake(0,            // temp value for x
                                0,            // temp value for y
                                height,       // width of image
                                scaledWidth); // height of image

   // adjust position of image view
   imageView.center = CGPointMake((bounds.size.width/3) + ((bounds.size.width/3) * col),
                                  ((height+10) * row) + (height+10)/2));

   // add image view to super view
   [rootView addSubView:imageView];
};
CGRect界限=[[UIScreen mainScreen]界限];
CGFloat maxCol=[imageViewArray count]+([imageViewArray count]%2);
CGFloat width=(bounds.size.width/2)-15;
CGFloat height=(bounds.size.height/maxCol)-10;
//循环浏览图像视图
用于(pos=0;pos<[imageViewArray count];pos++)
{
//计算网格内的位置
CGFloat行=(位置/2);
CGFloat col=(位置%2);
//检索UIImageView和UIImage
imageView=[imageViewArray对象索引:pos];
image=imageView.image;
//计算图像大小
CGFloat scaledWidth=(高度*image.size.width)/image.size.height;
//调整图像视图的大小
imageView.frame=CGRectMake(0,//x的临时值
0,//y的临时值
高度,//图像的宽度
scaledWidth);//图像的高度
//调整图像视图的位置
imageView.center=CGPointMake((bounds.size.width/3)+(bounds.size.width/3)*col),
((高度+10)*行)+(高度+10)/2));
//将图像视图添加到超级视图
[rootView addSubView:imageView];
};

上述缩放假设栅格位置的高度始终小于栅格位置的宽度。

这应该可以让您开始。然而,它没有考虑图像的比率

CGRect  bounds = [[UIScreen mainScreen] bounds];
CGFloat maxCol = [imageViewArray count] + ([imageViewArray count] % 2);
CGFloat width  = (bounds.size.width/2) - 15;
CGFloat height = (bounds.size.height/maxCol) - 10; 

// loop through imageViews
for(pos = 0; pos < [imageViewArray count]; pos++)
{
   // calculate position within grid
   CGFloat row = (pos/2);
   CGFloat col = (pos%2);


   // retrieves UIImageView and UIImage
   imageView       = [imageViewArray objectAtIndex:pos];
   image           = imageView.image;

   // calculates image size
   CGFloat scaledWidth = (height * image.size.width) / image.size.height;

   // adjust size of image view
   imageView.frame = CGRectMake(0,            // temp value for x
                                0,            // temp value for y
                                height,       // width of image
                                scaledWidth); // height of image

   // adjust position of image view
   imageView.center = CGPointMake((bounds.size.width/3) + ((bounds.size.width/3) * col),
                                  ((height+10) * row) + (height+10)/2));

   // add image view to super view
   [rootView addSubView:imageView];
};
CGRect界限=[[UIScreen mainScreen]界限];
CGFloat maxCol=[imageViewArray count]+([imageViewArray count]%2);
CGFloat width=(bounds.size.width/2)-15;
CGFloat height=(bounds.size.height/maxCol)-10;
//循环浏览图像视图
用于(pos=0;pos<[imageViewArray count];pos++)
{
//计算网格内的位置
CGFloat行=(位置/2);
CGFloat col=(位置%2);
//检索UIImageView和UIImage
imageView=[imageViewArray对象索引:pos];
image=imageView.image;
//计算图像大小
CGFloat scaledWidth=(高度*image.size.width)/image.size.height;
//调整图像视图的大小
imageView.frame=CGRectMake(0,//x的临时值
0,//y的临时值
高度,//图像的宽度
scaledWidth);//图像的高度
//调整图像视图的位置
imageView.center=CGPointMake((bounds.size.width/3)+(bounds.size.width/3)*col),
((高度+10)*行)+(高度+10)/2));
//将图像视图添加到超级视图
[rootView addSubView:imageView];
};

上述缩放假设网格位置的高度始终小于网格位置的宽度。

如果要加载大量图像,将导致性能问题。更好的方法是将
UITableView
与一起使用。

如果要加载大量图像,将导致性能问题。更好的方法是使用
UITableView
with.

谢谢,我尝试了你的代码,是的,它似乎是正确的。谢谢,我尝试了你的代码,是的,它似乎是正确的。要么我不理解你的建议,要么你不理解我的问题。我知道一行中的视图数(2)。所以在iphone中,每一列的宽度都是160,我的问题是,在每一列中,我需要将图像居中。要么我不理解你的建议,要么你不理解我的问题。我知道一行中的视图数(2)。所以在iphone中,每一列的宽度都是160,我的问题是,在每一列中,我需要将图像居中。是的,这是一个选项,但是在一个数据源上绑定两个iTen到每一行不是很奇怪吗?不,奇怪的是什么?另外,如果我谈论性能:您可以在
tableView:willDisplayCell:forrowatinexpath:
中从文件系统加载映像。我是iOS开发人员的新手,我的理解是uitableview的概念是拥有一个数据源,并将一个项绑定到每一行。这就是为什么我说将数据源中的两个元素绑定到每一行感觉很奇怪。但如果可以的话,那肯定是一个选择。谢谢,谢谢