图像加载表非常慢

时间:2013-09-04 10:37:04

标签: iphone ios objective-c xcode uitableview

我从服务器获取数据,其中包含我从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" 

图片正确加载但是加载该图片并滚动桌面视图需要一些时间它会非常慢我希望它能够快速完成。

4 个答案:

答案 0 :(得分:3)

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

根据许多开发人员的建议,请选择SDWebimage,它确实包含了下载图像文件的上述策略。您可以加载所需数量的图像,并且不会按照以下方式多次下载相同的网址代码作者

[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

答案 1 :(得分:1)

你必须异步加载图像,这样tableview可以在你的代码表视图中快速加载,这需要花费很多时间,因为它为每个单元格加载图像。

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

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

请使用您的网址更改网址

希望你的问题能够解决。

答案 2 :(得分:0)

从此链接下载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;
}

答案 3 :(得分:0)

另一个答案是使用惊人的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;
}
相关问题