標籤:ios sdwebimage
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];