我正在从服务器加载预览图像(缩略图)并将其保存到本地文档目录。对于每个图像都有一个核心数据条目。
现在我需要将此缩略图渲染到 ScrollView 中。我不知道将来会有多少缩略图,为什么我需要以编程方式将缩略图渲染到 ScrollView 中。我知道我也必须根据缩略图的数量设置 ScrollView 的高度。
缩略图需要可触摸,因为点击缩略图应该打开另一个对话框。
第一个问题:用于显示缩略图的正确控件是什么?是 UIButton 作为自定义按钮,缩略图设置为背景图像吗?
第二个问题:如何设置动态矩阵来渲染拇指(按钮)。
谢谢
最佳答案
第一个答案:我建议使用 UIButton
来实现此目的,这就是我在这种情况下会做的
第二个答案:假设您有某种所有缩略图的数组,那么您可以简单地迭代它们,以与此类似的方式创建所有按钮:
NSArray *thumbnailImages;
UIScrollView *scrollView;
//Scrollview is linked up through IB or created dynamically...whatever is easier for you
//The thumbnailImages array is populated by your list of thumbnails
//Assuming that your thumbnails are all the same size:
const float thumbWidth = 60.f;//Just a random number for example's sake
const float thumbHeight = 90.f;
//Determine how many thumbnails per row are in your matrix.
//I use 320.f as it is the default width of the view, use the width of your view if it is not fullscreen
const int matrixWidth = 320.f / thumbWidth;
const int contentHeight = thumbHeight * (thumbnailImages.count / matrixWidth);
for ( int i = 0; i < thumbnailImages.count; ++i )
{
int columnIndex = i % matrixWidth;
int rowIndex = i / matrixWidth;//Intentionally integer math
UIImage* thumbImage = [thumbnailImages objectAtIndex:i];
UIButton* thumbButton = [[UIButton alloc] initWithFrame:CGRectMake(columnIndex*thumbWidth, rowIndex*thumbHeight, thumbWidth, thumbHeight)];
thumbButton.imageView.image = thumbImage;
[scrollView addSubView:thumbButton];
[thumbButton release];
}
[scrollView setContentSize: CGSizeMake(320.f, contentHeight)];
不一定要逐字逐句地作为代码,我只是在记事本中写了这个,但它应该让您了解如何做到这一点
关于iOS 创建 UIButton 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8139603/