Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 图像加载表非常慢_Iphone_Ios_Objective C_Xcode_Uitableview - Fatal编程技术网

Iphone 图像加载表非常慢

Iphone 图像加载表非常慢,iphone,ios,objective-c,xcode,uitableview,Iphone,Ios,Objective C,Xcode,Uitableview,我正在从服务器获取数据,该服务器包含一些正确从url获取的图像,并将其设置为tablview单元格 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate]; sta

我正在从服务器获取数据,该服务器包含一些正确从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" 

图像加载正确,但加载该图像和滚动tableview将需要一些时间。它将非常慢。我希望它快一点。我如何才能做到这一点。

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

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

请使用您的url更改url


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

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

另一个答案是使用惊人的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;
}
#导入
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
AppDelegate*委托=(AppDelegate*)[[UIApplication sharedApplication]委托];
静态NSString*CellIdentifier=@“Cell”;
cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
cell.textlab.text=[delegate.firstArray objectAtIndex:indexath.row];
NSLog(@“%@”,代表第三方);
url=[NSURL URLWithString:[NSString stringWithFormat:@“%@,[delegate.thirdArray objectAtIndex:indexPath.row]];
[cell.imageView setImageWithURL:url];
//[cell.imageView setImageWithURL:url占位符图像:[UIImage ImageName:@“placeholder.png”];如果要添加占位符图像
返回单元;
}

使用
[NSURLConnection sendAsynchronousRequest:queue:completionHandler:
加载图像,然后使用NSCache防止反复下载同一图像

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

[NSURLConnection sendAsynchronousRequest:queue:completionHandler:

然后使用


进一步解释:

此问题与您的声誉水平不符…无论如何,尝试延迟加载…基尚,您能帮我解决此问题吗?我建议您首先完整地获取所有图像,然后将其添加到tableView中。检查我的答案您的问题将得到解决。我认为此问题至少会在每个da中提出一次ysynchronically在此上下文中是一个不正确的词。该词是synchronously。顺便说一句,您提到的链接建议OP异步加载图像。请编辑。感谢Amar的建议。:)我尝试了您的解决方案,效果很好,谢谢…非常欢迎:)请同步更改为异步…是的,AFNetworking的UIImageView扩展让这一切变得轻松愉快
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
}];