1,將下載好的第3方類庫SDWebImage源碼包加入到工程
2,進入工程的Build Phases,將源碼包裡面的所有.m文件全部添加到工程
3,導入第3方類庫依賴的兩個系統自帶的框架:MapKit.framework、ImageIO.framework
4,添加第3方類庫的主頭文件"UIImageView+WebCache.h"
代碼使用片段:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"Beyond";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
// 如果池中沒取到,則重新生成一個cell
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
}
// 設置cell中獨一無二的內容
Status *s = _statuses[indexPath.row];
cell.textLabel.text = s.text;
cell.detailTextLabel.text = s.user.screenName;
cell.textLabel.numberOfLines = 0;
// 重要~使用第3方框架 SDWebImage,緩存策略:失敗再請求,磁盤緩存,scrollView滾動時暫停下載圖片
[cell.imageView setImageWithURL:[NSURL URLWithString:s.user.profileImageUrl] placeholderImage:[UIImage imageNamed:@"avatar_default.png"] options:SDWebImageLowPriority | SDWebImageRefreshCached | SDWebImageRetryFailed];
// 返回cell
return cell;
}關鍵代碼,緩存策略:
失敗再請求:SDWebImageRetryFailed
磁盤緩存:SDWebImageRefreshCached
scrollView滾動時暫停下載圖片:SDWebImageLowPriority
[cell.imageViewsetImageWithURL:[NSURLURLWithString:s.user.profileImageUrl]placeholderImage:[UIImageimageNamed:@"avatar_default.png"]options:SDWebImageLowPriority |SDWebImageRefreshCached |SDWebImageRetryFailed];