gpt4 book ai didi

iphone - 仅显示几张图像,滚动不顺畅

转载 作者:行者123 更新时间:2023-12-03 21:21:25 25 4
gpt4 key购买 nike

我正在使用Apple展示的在表中使用 subview 的方法(下面的大部分内容来自他们的文档)。我只通过 rss feed 拉入大约 12 个图像,但这会导致滚动缓慢 - 如果我删除图像,它会平滑地移动。图片不大,所以这不是问题。在研究更多涉及的解决方案(后台处理等)之前,我可以做些什么来使这项工作更好吗?

感谢您对此提供的任何帮助。

#define MAINLABEL_TAG   1
#define SECONDLABEL_TAG 2
#define PHOTO_TAG 3

-(UITableViewCell *)tableView : (UITableView *)tableView cellForRowAtIndexPath : (NSIndexPath *)indexPath {
UILabel * mainLabel, * secondLabel;
UIImageView * photo;
static NSString * CellIdentifier = @ "Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(100.0, 0.0, 210.0, 0.0)] autorelease];
mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont systemFontOfSize:14.0];
mainLabel.textAlignment = UITextAlignmentRight;
mainLabel.textColor = [UIColor blackColor];
mainLabel.opaque = YES;
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview : mainLabel];

secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(90.0, 30.0, 220.0, 0.0)] autorelease];
secondLabel.tag = SECONDLABEL_TAG;
secondLabel.font = [UIFont systemFontOfSize:12.0];
secondLabel.textAlignment = UITextAlignmentRight;
secondLabel.textColor = [UIColor darkGrayColor];
secondLabel.opaque = YES;
secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview : secondLabel];

photo = [[[UIImageView alloc] initWithFrame:CGRectMake(30.0, 3.0, 50.0, 40.0)] autorelease];
photo.tag = PHOTO_TAG;
photo.opaque = YES;
photo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview : photo];
} else {
mainLabel = (UILabel *)[cell.contentView viewWithTag : MAINLABEL_TAG];
secondLabel = (UILabel *)[cell.contentView viewWithTag : SECONDLABEL_TAG];
photo = (UIImageView *)[cell.contentView viewWithTag : PHOTO_TAG];
}
// Configure the cell.
mainLabel.text = [[items objectAtIndex:indexPath.row] objectForKey:@ "title"];
secondLabel.text = [[items objectAtIndex:indexPath.row] objectForKey:@ "teacher"];
NSString * path = [[items objectAtIndex:indexPath.row] objectForKey:@ "audimage"];
NSString * mypath = [path stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL * url = [NSURL URLWithString:mypath];
NSData * data = [NSData dataWithContentsOfURL:url];
UIImage * img = [[UIImage alloc] initWithData:data];
photo.image = img;
return cell;
[cell release];
}

最佳答案

看起来每次配置单元格时,您都会从网络重新下载整个图像(或者从硬盘读取,如果您的 URL 指向的是硬盘):

NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];

这真的很慢! UITableView 的工作方式是重用单元格 - 每次单元格离开屏幕时,它都可能会返回给 cellForRowAtIndexPath 方法,用作下一个单元格可见。

这意味着每次新单元格变得可见时,您都会下载并创建其图像。

相反,您有两个选择:

  1. 先下载并存储所有图像(如果只有 12 个,可能不会占用太多内存)。

  2. 根据需要下载图像,如果收到内存警告,请转储一些缓存的图像。这实现起来有点困难,所以我会先尝试第一个解决方案。

关于iphone - 仅显示几张图像,滚动不顺畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4210940/

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