NSOperation 执行 uitableview 单元格图像

发布于 2024-11-13 07:44:17 字数 1240 浏览 3 评论 0原文


由于我是 iPad 应用程序开发新手,请帮助我解决简单的问题。
我想在服务器的表格视图中显示图像。它显示正确。
但是当我向上滚动表格并再次回到那里时,它将再次从服务器下载图像数据。
请帮我。

编辑:
tableView:cellForRowAtIndexPath:

static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 75)];
[imgView setImage:[UIImage imageNamed:@"strip_normal.png"]];
[imgView setHighlightedImage:[UIImage imageNamed:@"strip_hover.png"]];
[cell addSubview:imgView];
[imgView release];

UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 64, 64)];
imgView1.tag = indexPath.row;
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abc.com/abc/%@.png", [[arrMyCourses objectAtIndex:indexPath.row] valueForKey:@"CourseNumber"]]]]];
[imgView1 setImage:img];
[cell addSubview:imgView1];
[imgView1 release];

return cell;

提前致谢。

As i am new in iPad application development please help me in simple problem.
I want to display images in the tableview from the server. it is displaying correctly.
But when i scroll table upside and come back to again there it will again download image data from server.
Please help me.

EDIT:
tableView:cellForRowAtIndexPath:

static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 75)];
[imgView setImage:[UIImage imageNamed:@"strip_normal.png"]];
[imgView setHighlightedImage:[UIImage imageNamed:@"strip_hover.png"]];
[cell addSubview:imgView];
[imgView release];

UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 64, 64)];
imgView1.tag = indexPath.row;
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abc.com/abc/%@.png", [[arrMyCourses objectAtIndex:indexPath.row] valueForKey:@"CourseNumber"]]]]];
[imgView1 setImage:img];
[cell addSubview:imgView1];
[imgView1 release];

return cell;

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2024-11-20 07:44:17

您的问题是这一行

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abc.com/abc/%@.png", [[arrMyCourses objectAtIndex:indexPath.row] valueForKey:@"CourseNumber"]]]]];

您每次表视图请求单元格时都会下载图像,相信我这种情况经常发生,因为表视图不会在一次拍摄中构建整个视图。它重用屏幕外的单元来呈现新的可见单元。因此,一旦单元格离开屏幕并重新打开,就会再次调用 cellForRowAtIndexPath: 。因此图像会再次下载。您还会同步下载图像,这也会阻塞 UI。

要解决此问题,您应该考虑在开始时下载一次并将它们保存在本地临时位置,并根据需要将它们加载到内存中。将它们全部存储在内存中可能会付出高昂的代价。此外,使用 performSelectorInBackground:withObject 将下载移至后台。您必须将 UIKit 更新发送回主线程,否则您将遇到崩溃。

Your problem is this line

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abc.com/abc/%@.png", [[arrMyCourses objectAtIndex:indexPath.row] valueForKey:@"CourseNumber"]]]]];

You are downloading the image every time the table view requests for a cell and believe me this happens quite often as table view doesn't construct the entire view in a single shot. It reuses the cells going off screen to present new visible cells. So as soon as a cell goes off screen and comes back on, the cellForRowAtIndexPath: is called again. So the image gets downloaded once more. You are also downloading the image synchronously which will block the UI as well.

To fix this, you should consider downloading them once at the beginning and save them locally in a temporary location and load them into the memory as necessary. Having all of them in the memory can be costly. Additionally, move your download to the background using performSelectorInBackground:withObject. You will have to send the UIKit updates back to the main thread or else you will experience crashes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文