IOS网络图片缓存之SDWebImage

加载网络图片可以说是网络应用中必备的。如果单纯的去下载图片,而不去做多线程、缓存等技术去优化,加载图片时的效果与用户体验就会很差。

处理网络图片缓存步骤:

1、根据图片URL查找内存是否有这张图片,有则返回图片,没有则进入下一步。

2、查找本地磁盘存储是否有这张图片,有则返回图片,没有进行下一步。

3、从网络上下载该图片,下载完后保存到内存和本地磁盘存储上,并返回该图片。


使用第三方框架SDWebImage

特点:

1、依赖库很少,功能全面

2、自动实现磁盘缓存

3、缓存图片名字是以MD5进行加密的后缀名进行命名

4、只需要一个方法就可以实现多线程&带缓冲等效果


使用SDWebImage 方法实现缓存图片功能

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return self.appList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *ID = @"Cell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

	//用模型来填充每个cell
	XNApp *app = self.appList[indexPath.row];
	cell.textLabel.text = app.name;  //设置文字


	//使用SDWebImage来完成上面的功能. 针对ImageView.
	//一句话, 自动实现了异步下载. 图片本地缓存. 网络下载. 自动设置占位符.
	[cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"user_default"]];


	return cell;
}


SDWebImage中的一些参数 修改:

* SDWebImageRetryFailed = 1<< 0,    默认选项,失败后重试
* SDWebImageLowPriority = 1<< 1,     使用低优先级
* SDWebImageCacheMemoryOnly = 1<< 2,    仅仅使用内存缓存
* SDWebImageProgressiveDownload = 1<< 3,    显示现在进度
* SDWebImageRefreshCached = 1<< 4,     刷新缓存
* SDWebImageContinueInBackground =1 << 5,    后台继续下载图像
* SDWebImageHandleCookies = 1<< 6,     处理 Cookie
* SDWebImageAllowInvalidSSLCertificates= 1 << 7,     允许无效的 SSL 验证
* SDWebImageHighPriority = 1<< 8,      高优先级
* SDWebImageDelayPlaceholder = 1<< 9      延迟显示占位图片


猜你喜欢

转载自blog.csdn.net/u011043997/article/details/52164344