将图像异步加载到 UIImage 中 [英] Loading an image into UIImage asynchronously

查看:27
本文介绍了将图像异步加载到 UIImage 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 iOS 5.0 SDK 和 XCode 4.2 开发 iOS 4 应用程序.

I'm developing an iOS 4 application with iOS 5.0 SDK and XCode 4.2.

我必须将一些帖子博客显示到 UITableView 中.当我检索到所有 Web 服务数据时,我使用此方法创建一个 UITableViewCell:

I have to show some post blogs into a UITableView. When I have retreived all web service data, I use this method to create an UITableViewCell:

- (BlogTableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* cellIdentifier = @"BlogCell";

    BlogTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil)
    {
        NSArray* topLevelObjects =  [[NSBundle mainBundle] loadNibNamed:@"BlogTableViewCell" owner:nil options:nil];

        for(id currentObject in topLevelObjects)
        {
            if ([currentObject isKindOfClass:[BlogTableViewCell class]])
            {
                cell = (BlogTableViewCell *)currentObject;
                break;
            }
        }
    }

    BlogEntry* entry = [blogEntries objectAtIndex:indexPath.row];

    cell.title.text = entry.title;
    cell.text.text = entry.text;
    cell.photo.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:entry.photo]]];

    return cell;
}

但是这一行:

cell.photo.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:entry.photo]]];

太慢了(entry.photo 有一个 http url).

it is so slow (entry.photo has a http url).

有没有办法异步加载该图像?我认为这很困难,因为 tableView:cellForRowAtIndexPath 经常被调用.

Is there any way to load that image asynchronously? I think it is difficult because tableView:cellForRowAtIndexPath is called very often.

推荐答案

看一看 SDWebImage:

Take a look at SDWebImage:

https://github.com/rs/SDWebImage

这是一组很棒的类,可以为您处理所有事情.

It's a fantastic set of classes that handle everything for you.

蒂姆

这篇关于将图像异步加载到 UIImage 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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