异步图像下载到表中

发布于 2024-11-25 21:08:54 字数 207 浏览 2 评论 0原文

我有一个表视图,我想异步下载图标图像(100x75)到每一行。到目前为止,我已经遵循了很多教程,但我似乎无法弄清楚。我该怎么做呢?

是否有人建议使用标准 NSURLConnection API 来执行此操作,或者我应该使用在线可用的类/库之一来执行此操作?如果是这样,您有什么建议?

当然,我需要它快速、高效、不泄漏。我也不希望下载影响滚动。

谢谢你!

I have a table view and I'd like to download an icon image (100x75) to each row asynchronously. I've followed many tutorials so far but I can't seem to figure it out. How should I do it?

Does anyone recommend just doing it using the standard NSURLConnection API or should I use one of those classes/libraries that are available online to do so? If so, what do you recommend?

Of course, I need it to be fast, efficient and does not leak. I also don't want the downloading to affect the scrolling.

Thank you!

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

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

发布评论

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

评论(2

草莓酥 2024-12-02 21:08:54

我能想到的两个选项:

(1)使用ASIHTTPRequest

(2) 一个自定义实现,它生成一个线程,您可以在其中使用 NSURL/NSData 的组合加载图像。加载图像后,使用 performSelectorOnMainThread:withObject: 将其发送到主 UI 线程上的方法。

NSThread *t = [[NSThread alloc] initWithTarget:self selector:@selector(loadImage:) object:nil];
[t start];
[t release];

-(void) updateImage:(id) obj {
  // do whatever you need to do
}

-(void) loadImage:(id) obj {

  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSURL *url = [NSURL URLWithString:@"imageurl"];
  NSData *imageData = [[NSData alloc]initWithContentsOfURL:url]; 
  UIImage *image = [UIImage imageWithData:imageData];
  [imageData release];

  [self performSelectorOnMainThread:@selector(updateImage:) withObject:data waitUntilDone:YES];  
  [pool release];

}

Two options I can think of:

(1) Use ASIHTTPRequest.

(2) A custom implementation that spawns a thread in which you load the image using a combination of NSURL/NSData. Once the image is loaded, send it to a method on the main UI thread using performSelectorOnMainThread:withObject:.

NSThread *t = [[NSThread alloc] initWithTarget:self selector:@selector(loadImage:) object:nil];
[t start];
[t release];

-(void) updateImage:(id) obj {
  // do whatever you need to do
}

-(void) loadImage:(id) obj {

  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSURL *url = [NSURL URLWithString:@"imageurl"];
  NSData *imageData = [[NSData alloc]initWithContentsOfURL:url]; 
  UIImage *image = [UIImage imageWithData:imageData];
  [imageData release];

  [self performSelectorOnMainThread:@selector(updateImage:) withObject:data waitUntilDone:YES];  
  [pool release];

}
深者入戏 2024-12-02 21:08:54

我建议您使用 ASIHTTPRequest。它简单而且相当快。
这是文档的链接 - ASIHTTPRequest

编辑

您只需下载可见单元格的图像。
这是一个示例:

- (void)loadContentForVisibleCells
{
    NSArray *cells = [self.tableView visibleCells];
    [cells retain];
    for (int i = 0; i < [cells count]; i++) 
    { 
        ...
        // Request should be here
        ...
    }
    [cells release];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
{
    if (!decelerate) 
    {
        [self loadContentForVisibleCells]; 
    }
}

无论如何,您需要编写大量代码才能使事情顺利快速地运行。

I'd recommend you using ASIHTTPRequest. Its simple and pretty fast.
Here is a link to the documentation - ASIHTTPRequest

EDIT

You need to download images for visible cells only.
Heres a sample:

- (void)loadContentForVisibleCells
{
    NSArray *cells = [self.tableView visibleCells];
    [cells retain];
    for (int i = 0; i < [cells count]; i++) 
    { 
        ...
        // Request should be here
        ...
    }
    [cells release];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
{
    if (!decelerate) 
    {
        [self loadContentForVisibleCells]; 
    }
}

Anyway u'll need to code a lot to make things work good and fast.

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