来自 URL(RSS 阅读器)的图像在表格视图中显示 - 问题

发布于 2024-11-10 09:47:24 字数 1901 浏览 0 评论 0原文

我在使用表格视图和一些自定义单元格时遇到问题。这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForRowAtIndexPath");
static NSString *MyIdentifier = @"customCell";

EventTableCell *cell = (EventTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {
    //cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    [[NSBundle mainBundle] loadNibNamed:@"EventTableCell" owner:self options:nil];
    cell = self.eventCell;
}

//Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
[[cell eventNameLabel] setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
[[cell eventDateLabel] setText:[[stories objectAtIndex: storyIndex] objectForKey: @"date"]];

NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];

// clean up the link - get rid of spaces, returns, and tabs...
storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:@"  " withString:@""];

//NSString *string = [[NSString alloc] stringWithString:[[stories objectAtIndex: storyIndex] objectForKey: @"link"]];
NSURL *url = [NSURL URLWithString:storyLink];
NSData *data = [[NSData alloc] initWithContentsOfURL: url];
UIImage *image = [UIImage imageWithData: data];
[[cell eventImage] setImage:image];

return cell;
}

我的表格中有很多这样的自定义单元格,超过了视图可以一次显示的数量。因此,我需要向上/向下滚动才能查看所有单元格。我的问题是,当我滚动时,它会滞后很多,并且在调试器控制台中,当每个新单元格进入视图时,它会调用 cellForRowAtIndexPath 。每个单元格都包含一个从 URL 下载的图像,因此必须转换为 UIImage。我相信这就是造成滞后的原因。

谁能指导我需要做什么才能下载图像并将它们显示在单元格中而不导致应用程序出现严重延迟?

IE:我应该将下载/转换代码放在哪里,以便在需要显示单元格之前图像已经存储为图像?

谢谢,

杰克

I'm having a problem with a table view and some custom cells. Here is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForRowAtIndexPath");
static NSString *MyIdentifier = @"customCell";

EventTableCell *cell = (EventTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (cell == nil) {
    //cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
    [[NSBundle mainBundle] loadNibNamed:@"EventTableCell" owner:self options:nil];
    cell = self.eventCell;
}

//Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
[[cell eventNameLabel] setText:[[stories objectAtIndex: storyIndex] objectForKey: @"title"]];
[[cell eventDateLabel] setText:[[stories objectAtIndex: storyIndex] objectForKey: @"date"]];

NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];

// clean up the link - get rid of spaces, returns, and tabs...
storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:@"\n" withString:@""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:@"  " withString:@""];

//NSString *string = [[NSString alloc] stringWithString:[[stories objectAtIndex: storyIndex] objectForKey: @"link"]];
NSURL *url = [NSURL URLWithString:storyLink];
NSData *data = [[NSData alloc] initWithContentsOfURL: url];
UIImage *image = [UIImage imageWithData: data];
[[cell eventImage] setImage:image];

return cell;
}

My table has a lot of these custom cells in it, more than the view can show at once. So therefore I need to scroll up/down to see all the cells. My problem is this, when I scroll it lags a lot and in the debugger console it calls cellForRowAtIndexPath as each new cell comes into the view. Each cell holds an image which is downloaded from a URL and hence has to be converted to a UIImage. I believe this is whats causing the lag.

Can anyone direct me as to what I need to do in order to download the images and have them display in the cells without causing major lag in the app?

IE: Where would I put the download/conversion code so that the image is already stored as an image before the cell needs to be dispayed?

Thanks,

Jack

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

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

发布评论

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

评论(1

盗梦空间 2024-11-17 09:47:24

如果您想预先下载图像,您可以在应用程序中的任何位置执行此操作并缓存它们以供以后使用。例如,您可以在下载并解析 RSS 源后立即开始下载。您可以将它们保存在 [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] 返回的目录中。最好使用异步方法来下载它们,并且一次只有几个下载活动以获得最佳性能。

如果您想根据需要加载它们,请在分配单元格时设置占位符图像并使用异步 NSURLConnection 或类似的文件来进行下载。下载完成后,将占位符替换为真实图像(当然,并将其缓存以供以后重用)。

If you want to pre-download the images, you could do it at any point in your application and cache them for later. For example, you could start the downloads just after you download and parse the RSS feed. You can save them in the directory returned by [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]. It would be best to use asynchronous method to download them, and have just a few downloads active at a time for best performance.

If you want to load them as needed, set a placeholder image when you allocate the cell and use an asynchronous NSURLConnection or the like to do the download. Once the download completes, replace the placeholder with the real image (and cache it for reuse later, of course).

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