iOS异步加载图片

    从网上下载图片是很慢的,为了不影响体验,选择延时加载图片是很好的办法。

一个 tableView 列表,左边暂时没有图

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    Tweet *tempTweet = [tweets objectAtIndex:indexPath.row];
    if (tempTweet.imgData == nil) {//没有头像
        if (self.tableTweets.dragging == NO && self.tableTweets.decelerating == NO) {//不在拖动中和减速时,开始下载图片
            ImgRecord *record = [[ImgRecord alloc] init];
            record.url = tempTweet.img;
            [self startIconDownload:record forIndexPath:indexPath];
        }
        cell.imageView.image = [UIImage imageNamed:@"avatar_loading.jpg"];//占位图片(等待下载)
    }else{//已经存在图片
        cell.imageView.image = tempTweet.imgData;
    }
    
    cell.textLabel.text = tempTweet.tweet;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@   于%@",tempTweet.author,tempTweet.fromNowOn];
    return cell;
}

关键就是 [self startIconDownload:appRecord forIndexPath:indexPath];

- (void)startIconDownload:(AppRecord *)appRecord forIndexPath:(NSIndexPath *)indexPath

{

    NSString *key = [NSStringstringWithFormat:@"%d",[indexPathrow]];

    IconDownloader *iconDownloader = [imageDownloadsInProgressobjectForKey:indexPath];

    if (iconDownloader ==nil)//已经在下载中的不用重复下载了,没有在下载中就往下走

    {

        iconDownloader = [[IconDownloader alloc] init];

        iconDownloader.imgRecord = imgRecord;

        iconDownloader.index = key;

        iconDownloader.indexPathInTableView = indexPath;

        iconDownloader.delegate = self;

        [imageDownloadsInProgress setObject:iconDownloader forKey:key];//赋值

        [iconDownloader startDownload];

        [iconDownloader release];

    }

}


IconDownloader  是一个下载图片封装类
关键方法: iconDownloader.delegate = self;
[iconDownloader startDownload];

一个是委托,将来告诉self下载完成更新图片
一个是自己方法开始联网下载图片

委托调用方法,重设图片

- (void)appImageDidLoad:(NSString *)index//代理(重设图片

{

    Tweet *t = [tweetsobjectAtIndex:[indexintValue]];

    IconDownloader *iconDownloader = [imageDownloadsInProgressobjectForKey:index];//取值

    if (iconDownloader != nil)//如果有头像

    {

        UITableViewCell *cell = [self.tableTweetscellForRowAtIndexPath:iconDownloader.indexPathInTableView];

        cell.imageView.image = iconDownloader.imgRecord.img;

        t.imgData = iconDownloader.imgRecord.img;//把图片赋到相应位置

    }

}


IconDownloader 中的方法

- (void)startDownload

{

self.activeDownload = [NSMutableData data];


NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:

[NSURLRequest requestWithURL:

[NSURL URLWithString:appRecord.imageURLString]] delegate:self];

self.imageConnection = conn;

[conn release];

}


最后 NSURLConnection 的委托需要自己实现了。
运行后效果图
可能后来由于开源中国的服务器问题,文件后来解析不了。但代码是没有问题的。
今天发现又能用了,把之前的改动了一个。
下一步加入缓存,从缓存中提取。
参考:http://www.alivenotdead.com/xiaoxin5518/iOS--profile-1883698.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值