SDWebImage的三個常用方法
//賦值網(wǎng)絡圖片:
NSString * imageString = [NSString stringWithFormat:@"%@%@",baseUrl,shop.shop_image];
[cell.iconView sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"defaultImage"]];
[cell.iconView sd_setImageWithURL:[NSURL URLWithString:imageString] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"下載完成");
}];
//SDWebImage提供了獲取下載進度的方法:
[cell.iconView sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"defaultImage"] options:SDWebImageRetryFailed | SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//獲取圖片下載進度:
NSLog(@"%ld" , receivedSize / expectedSize);
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"下載完成");
}];
#import "ViewController.h"
#import "Masonry.h"
#import "ShopCell.h"
#import "UIImageView+WebCache.h" //導入SDWebImage
static NSString * baseUrl = @"http://192.168.XX.XX/images/"; //網(wǎng)絡接口地址
@interface ViewController ()
@property (nonatomic, strong) NSArray * datalist;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[ShopCell class] forCellReuseIdentifier:NSStringFromClass([ShopCell class])];
self.tableView.rowHeight = 100;
}
- (NSArray *)datalist
{
if (!_datalist)
{
_datalist = [ShopEntity loadShopData];
}
return _datalist;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.datalist.count;
}
#pragma mark - <UITableViewDataSource>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ShopCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ShopCell class])];
ShopEntity * shop = self.datalist[indexPath.row];
//賦值主標題:
cell.nameLabel.text = shop.shop_name;
//賦值副標題:
cell.descLabel.text = shop.shop_desc;
//賦值網(wǎng)絡圖片:
NSString * imageString = [NSString stringWithFormat:@"%@%@",baseUrl,shop.shop_image];
[cell.iconView sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"defaultImage"]];
[cell.iconView sd_setImageWithURL:[NSURL URLWithString:imageString] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"下載完成");
}];
//SDWebImage提供了獲取下載進度的方法:
[cell.iconView sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:@"defaultImage"] options:SDWebImageRetryFailed | SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//獲取圖片下載進度:
NSLog(@"%ld" , receivedSize / expectedSize);
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSLog(@"下載完成");
}];
return cell;
}
#pragma mark - <UITableViewDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//options 圖片緩存策略
/*
options所有選項:
//失敗后重試
SDWebImageRetryFailed = 1 << 0,
//UI交互期間開始下載搁痛,導致延遲下載比如UIScrollView減速谍肤。
SDWebImageLowPriority = 1 << 1,
//只進行內(nèi)存緩存
SDWebImageCacheMemoryOnly = 1 << 2,
//這個標志可以漸進式下載,顯示的圖像是逐步在下載
SDWebImageProgressiveDownload = 1 << 3,
//刷新緩存
SDWebImageRefreshCached = 1 << 4,
//后臺下載
SDWebImageContinueInBackground = 1 << 5,
//優(yōu)先下載
SDWebImageHighPriority = 1 << 8,
//延遲占位符
SDWebImageDelayPlaceholder = 1 << 9,
//改變動畫形象
SDWebImageTransformAnimatedImage = 1 << 10,
*/
@end