【问题标题】:Images load table is very slow [duplicate]图像加载表非常慢[重复]
【发布时间】:2013-09-04 10:37:04
【问题描述】:

我正在从服务器获取数据,其中包含一些我从 url 正确获取的图像并将其正确设置到 tablview 单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    static NSString *CellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text=[delegate.firstArray objectAtIndex:indexPath.row];

    NSLog(@"%@",delegate.thirdArray);

    url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[delegate.thirdArray objectAtIndex:indexPath.row]]];

    data = [[NSData alloc]initWithContentsOfURL:url];

    UIImage * img=[UIImage imageWithData:data];

    cell.imageView.image=img;

    return cell;
}

在 delegate.thirdArray 中包含这个 url

 "http://awe.com/app/images/can.png",
"http://awe.com/app/images/card.png",
"http://awe.com/app/images/tp.png",
"http://awe.com/app/images/tricks.png" 

图像加载正常,但加载该图像并滚动表格视图需要一些时间,它会非常慢我希望它快我该怎么做。

【问题讨论】:

  • 此问题与您的信誉级别不匹配...无论如何尝试延迟加载...
  • h kishan 你能帮我解决这个问题吗..
  • 我建议你先完整获取所有图像,然后将它们添加到 tableView..
  • 查看我的回答您的问题将得到解决
  • 我认为这个问题每天至少被问一次

标签: iphone ios objective-c xcode uitableview


【解决方案1】:

使用[NSURLConnection sendAsynchronousRequest:queue:completionHandler: 加载您的图像,然后使用 NSCache 防止一次又一次地下载相同的图像。

正如许多开发人员所建议的那样,请使用SDWebimage,它确实包含上述​​下载图像文件的策略。您可以加载任意数量的图像,并且不会按照作者的说法多次下载相同的 URL代码

[NSURLConnection sendAsynchronousRequest:queue:completionHandler:

上的示例
NSURL *url = [NSURL URLWithString:@"your_URL"];
NSURLRequest *myUrlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest: myUrlRequest queue: queue completionHandler: ^ (NSURLResponse *response, NSData *data, NSError *error)
{

    if ([data length] > 0 && error == nil)
        //doSomething With The data

    else if (error != nil && error.code == ERROR_CODE_TIMEOUT)
        //time out error

    else if (error != nil)
        //download error
}];

然后使用NSCache...

进一步解释:READ HERE

【讨论】:

    【解决方案2】:

    您必须异步加载图像,以便表格视图可以在您的代码中快速加载表格视图需要很多时间,因为它会为每个单元格加载图像。

    请查看下面的答案,您可以找到如何异步加载图像。

    https://stackoverflow.com/a/15377082/1713478

    请用您的网址更改网址

    希望您的问题能得到解决。

    【讨论】:

    • synchronically 在这种情况下是一个不正确的词。这个词是同步。顺便说一句,您提到的链接建议 OP 异步加载图像。请编辑。
    • 感谢 Amar 的建议。 :)
    • 我尝试了你的解决方案,对我来说很好,谢谢...
    • 不客气 :)
    • 请同步改为异步...
    【解决方案3】:

    从此链接下载 AFNetworking

    https://github.com/AFNetworking/AFNetworking

    然后将文件添加到您的项目中,然后在 .m 文件中

    #import "UIImageView+AFNetworking.h"
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    
        static NSString *CellIdentifier = @"Cell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        cell.textLabel.text=[delegate.firstArray objectAtIndex:indexPath.row];
    
        NSLog(@"%@",delegate.thirdArray);
    
        url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[delegate.thirdArray objectAtIndex:indexPath.row]]];
    
    
    
        [cell.imageView setImageWithURL:url];
    
        return cell;
    }
    

    【讨论】:

    • 是的,AFNetworking 的 UIImageView 扩展让这一切变得简单而轻松
    【解决方案4】:

    另一个答案是使用令人惊叹的 SDWebImage 框架。它将异步加载您的图像并缓存它们,因此如果用户必须再次加载相同的图像,他将不必再次下载。

    这是框架的链接:

    https://github.com/rs/SDWebImage
    

    这是您单元格的代码:

    #import <SDWebImage/UIImageView+WebCache.h>
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    
        static NSString *CellIdentifier = @"Cell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        cell.textLabel.text=[delegate.firstArray objectAtIndex:indexPath.row];
    
        NSLog(@"%@",delegate.thirdArray);
    
        url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[delegate.thirdArray objectAtIndex:indexPath.row]]];
    
        [cell.imageView setImageWithURL:url];
        // [cell.imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; if you want to add a placeholder image
    
        return cell;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 2014-04-09
      • 2019-10-19
      • 2012-08-19
      • 2015-02-03
      • 1970-01-01
      相关资源
      最近更新 更多