在 UITableView 中从 Web 加载图像的更有效方法是什么? [英] What is the more efficient way of loading an image from the web in a UITableView?

查看:23
本文介绍了在 UITableView 中从 Web 加载图像的更有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的 UITableView 中的每个 UITableViewCell,我在后台线程中从网络下载一个图像.然后我使用主线程从单元格更新 imageView.

for each UITableViewCell in my UITableView, I download an image from the web in the background thread. Then I update the imageView from the cell using the main thread.

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
                //Background Thread
                // download the image in separate thread
                UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: @"someurl.com" ] ] ];
                dispatch_async(dispatch_get_main_queue(), ^(void){
                    // this is called in the main thread after the download has finished, here u update the cell's imageView with the new image
                    cell.eventImageView.image = image;
                });
            });

滚动时我没有注意到任何滞后.

I dont notice any lags when scrolling.

但是,我想知道是否有更好的方法来做到这一点,如果有,我该怎么做?

However, I like to know if there is a better way of doing this, and if so, how can I do it?

谢谢

推荐答案

我更喜欢使用 SDWebImage(可以在这里找到git)

I prefer using SDWebImage (can be found here on Git)

我写的页面听起来和你的很相似,而且代码非常简单:

I have written pages which sound similar to yours and it involves very simple code:

    // Set and load the images
    [cell.menuImageView sd_setImageWithURL:url placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

        // Get rid of the activity indicator when the image has been loaded
        [activity stopAnimating];
    }];

在我看来,有许多优点:

There are, in my opinion, a number of advantages:

1) 该库很灵活,并为您提供了很多关于如何下载图像的选项(这些只是少数)

1) The library is flexible and gives you lots of options on how you want to download the image (these are only a few)

例如:

- (void)sd_setImageWithURL:(NSURL *)url
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options
- (void)sd_setImageWithURL:(NSURL *)url completed:(SDWebImageCompletionBlock)completedBlock
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock

2) 该库会缓存图像,这意味着您下次不必再次下载它们.

2) The library caches the images meaning that the next time you don't have to download them again.

3) 该库得到了很好的支持,有助于解决问题

3) The library is well supported helping to trouble shoot issues

我个人开始使用您使用的技术,发现存在局限性和困难(如果图像下载速度缓慢,则添加活动指示器等),然后试用了许多不同的异步图像下载器,结果这是最好的

I personally started using the technique you used and found there were limitations and difficulties (adding activity indicators if the image was slow downloading etc) and then trialled a number of different asynchronous image downloaders and this one came out as the best

希望能帮到你

这篇关于在 UITableView 中从 Web 加载图像的更有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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