设为首页 加入收藏

TOP

【原】SDWebImage源码阅读(一)(一)
2017-10-12 12:06:51 】 浏览:10427
Tags:SDWebImage 源码 阅读

【原】SDWebImage源码阅读(一)

本文转载请注明出处 —— polobymulberry-博客园

1. 前言


一直没有系统地读过整套源码,就感觉像一直看零碎的知识点,没有系统读过一本专业经典书籍一样,会有点发虚,感觉知识体系不健全!废话少说,这次我决定好好阅读下SDWebImage的源码,我的阅读方式,是带着问题去阅读源码,然后强迫自己写博客

2. SDWebImage是做什么的?


既然是要带着问题读,那么第一个问题就来了,SDWebImage是做什么的?SDWebImage是一个开源的代码库,我们可以在github上找到它 —> Github传送门

Github上是这样介绍它的:

This library provides a category for UIImageView with support for remote images coming from the web.

所以我们大概知道SDWebImage就是一个库。这个库本质是UIImageView的category。为啥要做这个category呢?是为了从服务器端远程获取图片到UIImageView上显示。当然,看完代码后,就知道SDWebImage提供的功能远不止说的这么简单。

3. SDWebImage怎么用?


github上也给了一些例子,我们看一下最常用的一个例子:

 1 #import <SDWebImage/UIImageView+WebCache.h>
 2 
 3 ...
 4 
 5 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 6     static NSString *MyIdentifier = @"MyIdentifier";
 7 
 8     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
 9     if (cell == nil) {
10         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
11                                        reuseIdentifier:MyIdentifier] autorelease];
12     }
13 
14     // Here we use the new provided sd_setImageWithURL: method to load the web image
15     [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
16                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
17 
18     cell.textLabel.text = @"My Text";
19     return cell;
20 }

这确实是一个很常见的需求,就是在一个tableView上,每一个cell都需要显示网络端获取的image。比如我们常用的新浪微博、网易新闻、知乎日报等等,都会用到。

这里最关键的一行代码就是:

1     // Here we use the new provided sd_setImageWithURL: method to load the web image
2     [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
3                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

看到这里,我情不自禁地要赞叹两句,这个接口设计的真的很棒!你想想,我要从网络端获取图片,并显示到UIImageView上,其实我只要给你一个图片的url就ok啦,另外当前图片如果还未获取到,怎么办?弄个placeholderImage呗(当网络端图片还未加载完成,作为一个替代的图片,比如一些app如果网络不好的话,文章对应图片加载不出来,就会显示带有“暂无图片”的图片)。

其中的图片如何获取,如何缓存等等都屏蔽了。甚至没有暴露从网络端获取到的是什么图片,当然后面我们会提到SDWebImage中有其他的借口会暴露返回的图片image,允许在image上操作后再赋值给imageView。

细想下其中的过程,我大体有一个简单的实现概念(先自己想想怎么实现,然后对照实际源码,这样才能看到自己不足):

  1. 先将UIImageView的image设为placeholderImage
  2. 然后发出网络请求,获取图片image

  3. 如果图片获取成功,赋值给UIImageView

带着我这简陋的想法,我模仿SDWebImage写了如下代码:

首先我创建了一个UIImageView的category —— UIImageView+Extension.h

主要是模仿SDWebImage的 sd_setImageWithURL:placeholderImage:函数写了一个pjx_setImageWithURL:placeholderImage:

- UIImageView+Extension.h

1 #import <UIKit/UIKit.h>
2 
3 @interface UIImageView (Extension)
4 
5 - (void)pjx_setImageWithURL:(NSURL *)imageUrl placeholderImage:(UIImage *)placeholderImage;
6 
7 @end

- UIImageView+Extension.m

 1 #import "UIImageView+Extension.h"
 2 
 3 @implementation UIImageView (Extension)
 4 
 5 - (void)pjx_setImageWithURL:(NSURL *)imageUrl placeholderImage:(UIImage *)placeholderImage
 6 {
 7     // 1.先将UIImageView的image设为placeholderImage
 8     self.image = placeholderImage;
 9     
10     // 2.然后发出网络请求,获取图片image
11     NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
12     UIImage *image = [UIImage imageWithData:imageData];
13     
14     // 3.如果图片获取成功,赋值给UIImageView
15     if (image) {
16         self.image = image;
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇UINavigationController 导航控制.. 下一篇一个裁剪图片的小工具类,通过一句..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目