-4

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

4 回答 4

3

使用加载图像[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 ...

如需进一步说明:请阅读此处

于 2013-09-04T10:58:07.010 回答
1

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

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

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

请用您的网址更改网址

希望你的问题能解决。

于 2013-09-04T10:41:32.063 回答
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;
}
于 2013-09-04T10:47:20.390 回答
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;
}
于 2013-09-04T10:57:31.147 回答