gpt4 book ai didi

ios - 具有大量图像的 UITableView

转载 作者:行者123 更新时间:2023-11-28 23:33:11 25 4
gpt4 key购买 nike

我的应用需要在 UITableView 中显示大量图像(大约 2000 张)。基本上,我使用以下代码构建我的 UITableViewCell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}

// Some Operations...

NSString *path = [self.dataArray jk_objectWithIndex:indexPath.row];
UIImage *img = [UIImage imageWithContentsOfFile:path];
cell.imageView.image = img;

return cell;
}

这有效,但是当加载 tableview 时,内存增加很快,似乎所有图像都加载到内存中。

有什么好主意可以解决这个问题吗?我只是想保存内存。

顺便说一句,有谁知道实现这种需求的常用方法是什么?我认为将所有图像加载到内存是最愚蠢的方法......我初始化 tableview 行的代码如下:


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if (!_isLoading) {
return self.dataArray.count; // about 2000... That's terrible
} else {
return 0;
}
}

谢谢!

最佳答案

您的代码有两个问题。

  • 首先,也是最重要的一点,图像很大,但表中图像的显示很小。那是错误的。您应该仅以实际显示所需的尺寸加载图像。

  • 其次,默认情况下会缓存图像。您需要防止在加载这些图像时对其进行缓存。

您可以使用 ImageIO 框架在 cellForRowAt 中轻松完成这两件事。

关于ios - 具有大量图像的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55790179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com