如何做多线程加载图像更快地到tableView? [英] how to do multi-threading to load the image faster to tableView?

查看:78
本文介绍了如何做多线程加载图像更快地到tableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载图像到tableView,每次函数

   - (UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

p>

我的问题是它花了这么多时间来加载图像。



我的代码是..

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
self .tableView1 = tableView;
static NSString * simpleTableIdentifier = @SimpleTableCell;
SimpleTableCell * cell =(SimpleTableCell *)[tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if(cell == nil)
{
NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@SimpleTableCellowner:self options:nil];
cell = [nib objectAtIndex:0];
}

cell.nameLabel.text = title;

NSString * imageURL = [NSString stringWithFormat:@http://www.xyz.com/image1.png];

cell.thumbnailImageView.image = [UIImage imageWithData :[NSData dataWithContentsOfURL:[NSURL URLWithString:fullURL]]];;
}
return cell;
}

每次图片网址都会改变,并且加载每个图片需要时间。



可以任何人提出任何想法解决这个问题
多线程如何工作这个代码
在哪里和我应该在代码中编辑什么

解决方案

您必须在后台下载图片:

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString * simpleTableIdentifier = @SimpleTableCell;
SimpleTableCell * cell =(SimpleTableCell *)[tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0ul);
dispatch_async(queue,^ {

NSString * u = [NSString stringWithContentsOfFile:r.url encoding:NSUTF8StringEncoding error:nil];
NSURL * imageURL = [NSURL URLWithString:u ];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
dispatch_sync(dispatch_get_main_queue(),^ {
SimpleTableCell * cell =(SimpleTableCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.thumbnailImageView.image = [UIImage imageWithData:imageData];
[cell setNeedsLayout];

});
});

}

return cell;
}

只需在表视图方法中使用上述代码,你。


I am loading the image to the tableView, each time function

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

is executed, different url of image will come..

My problem is its taking so much time to load the image..

My code is..

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.tableView1 = tableView;
    static NSString *simpleTableIdentifier = @"SimpleTableCell";
    SimpleTableCell *cell = (SimpleTableCell *)[tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

    cell.nameLabel.text = title;

    NSString *imageURL = [NSString stringWithFormat: @"http://www.xyz.com/image1.png];

     cell.thumbnailImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:fullURL]]];
}
return cell;
}

every time the image url will change and it will take time to load each image..

can any one suggest any idea to solve this problem? how multithreading will work with this code? where and what should i edit in the code?

解决方案

You have to download image in background like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *simpleTableIdentifier = @"SimpleTableCell";
    SimpleTableCell *cell = (SimpleTableCell *)[tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier];   
         dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
        dispatch_async(queue, ^{

            NSString *u=[NSString stringWithContentsOfFile:r.url encoding:NSUTF8StringEncoding error:nil];
            NSURL *imageURL=[NSURL URLWithString:u];
            NSData *imageData=[NSData dataWithContentsOfURL:imageURL];
            dispatch_sync(dispatch_get_main_queue(), ^{
                SimpleTableCell *cell = (SimpleTableCell *)[tableView cellForRowAtIndexPath:indexPath];
                 cell.thumbnailImageView.image=[UIImage imageWithData:imageData];
                [cell setNeedsLayout];

            });
        });

    }

    return cell;
}

Just use above code in your table view method with required edit this should work for you.

这篇关于如何做多线程加载图像更快地到tableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆