使用SDWebImage

SDWebImage是一个非常好用及强大的图片异步下载类库,可以方便地为UITableViewcell等控件加载远程图片。SDWebImage类库支持对图片进行缓存,图片占位等功能。SDWebImage类库是以分类的形式整合到UIImageView类的,在导入UIImageView+WebCache类后,UIImageView类就拥有了setImageWithURL扩展方法。该方法即可实现图片下载及缓存功能。下面是详细的使用步骤。

1、部署SDWebImage

首先下载SDWebImage类库,点击此处下载。

下载完成后,将其拖入当前项目里。然后分别添加MapKit、ImageIo依赖包。如下图所示。


使用SDWebImage_第1张图片

2、使用SDWebImage

SDWebImage类库的使用非常简单,如以下代码所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma mark --表格单元格数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     NSString *identifier=@ "mycell" ;
                               
     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
     if (cell==nil) {
         NSString *str=[data_ objectAtIndex:indexPath.row];
         NSLog(@ "数据==%@n" ,str);
         cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];        UIImageView *imaegView=[[[UIImageView alloc] init] autorelease];
         imaegView.frame=CGRectMake(0, 2, 50, 50);
         [imaegView setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@ "nopic.png" ]];
         [cell.contentView addSubview:imaegView];
     }
                               
     return cell;
                               
}

你可能感兴趣的:(软件,ios开发,iPhone开发,苹果,开源技术)