A, SDWebImage Introduction Principles
1. Structure (reference design ideas: to provide a variety of interfaces, the low-level calls to the same method, to reduce the transmission of the caller optional parameter)
UIImageView + WebCache and UIButton + WebCache UIKit framework directly to the surface of the interface,
SDWebImageManger (SDWebImageManager is SDWebImage core classes, which we often come into contact class) is responsible for handling and coordinating SDWebImageDownloader and SDWebImageCache, and interact with UIKit layer. SDWebImageDownloaderOperation actually perform the download request;
SDWebImageManger Detailed: https://www.jianshu.com/p/246f6303cb6f
The bottom two classes provide support for high-level abstraction.
SDWebImageCompat is the most basic configuration file for each platform compatible with Apple
SDWebImageCompat Detailed: https://www.jianshu.com/p/cfdd59d7df7c
SDWebImageDecoder sd decoded picture of this class is actually a classification of UIImage UIImage + ForceDecode, mainly used to decode UIImage
SDWebImageDecoder Detailed: https://www.jianshu.com/p/e631b4b2a214
Structure Diagram
Structural details (FIG class)
2. Principle
Load a picture of the whole process:
- We use
SDWebImage
to call when this simple classification, and then quietly waiting for the picture is set to the class object.[imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]];
UIImageView
- After a series of calls, we first came to the
UIView+WebCache
classification, in this category, first of all to protect the uniqueness of image loading, and then began the core of the loading operation. - Then entered the
SDWebImageManager
class, in this class, the first to find whether there is a cache, no cache, then download it to the server. - Want to find the cache we want to enter
SDImageCache
this class, in this class, first to see if there is a corresponding memory cache, if not go to the hard drive to find out if there is a corresponding cache, but retrieved from the hard disk data picture , to get pictures but also through decoding, scaling and decompression. Of course, if no cache, then went to download. - Is responsible for downloading
SDWebImageDownloader
this class, a class in this class, the picture of the download operation packaged in a customSDWebImageDownloaderOperation
, then added to the operation in the queue. - When the operation queue calls this operation, the operation will call the object
- (void)start
methods, rewriting of this method to generate a task objectdataTask
and callresume
start the task. - Because the
SDWebImageDownloaderOperation
class comply with thedataTask
protocol object, so thedataTask
results of the execution will be carried out through a proxy callback method. In the proxy methods, obtains and stores the data returned by the server, and after the end of the task execution, data decoding, scaling and decompression. After the process is completed callback. - By numerous callbacks to callback data along
SDWebImageDownloaderOperation
->SDWebImageDownloader
->SDWebImageManager
->UIView+WebCache
all the way flow, which flows into theSDWebImageManager
middle when the picture cache, and finallyUIView+WebCache
forUIImageView
setting a good picture processing. - This is the
SDWebImage
loading process of the general picture, of course, there are a lot of small details and small features, just after reading these specific class learning.
Sequence of Operation
Operational flow chart
3. Source Interpretation (Comments can see where links)
Detailed: https://juejin.im/post/5adff5636fb9a07aa213026b
Two, SDWebImage4.0,5.0 contrast
4.0源码解析 :https://juejin.im/post/5bbec080e51d450e4d3021c0
5.0源码解析:https://www.jianshu.com/p/8af8fc7cd5bc
1.项目中结构(还是一样的,不像网上资料说的 变更了结构)
2.文件对比 5.0比4.0多出 61个文件(114+2-55)
3.SDAnimated 4.0只是为了处理GIF 5.0可以处理动画
4.Cache 充分体现SRP(单一职责)开发原则 ,缓存处理更加完备 专门的磁盘缓存处理类 内存缓存处理类
SDImageCache是SDWebImage的缓存类,是SDWebImage的重要组成部分,主要提供图片的缓存,和提取,分为内存缓存和磁盘缓存两种,因为SDWebImage的接口性设计,SDImageCache完全可以单独拿出来使用
详解:https://www.jianshu.com/p/f8f724bb0f4b
5.Coder(图片编解码器) 方法命名少了Web
SDWebImage里自己写了一个编解码管理器,用于实现编码,解码,压缩,缩小图片像素功能。涉及到的文件有SDWebImageCodersManager,SDWebImageCoder,SDWebImageImageIOCoder等等
详解 :https://www.jianshu.com/p/615d3178a508
6.Loader 图片下载 充分体现SRP(单一职责)开发原则,区分更加明显 类的功能更加单一 ,SDWebImageError 4.0没有 5.0专门的错误处理
SDWebImageDownloader和SDWebImageDownloaderOperation 是 SDWebImage的下载类,是SDWebImage的很重要的一个类,这个类主要负责图片的下载
详解:https://www.jianshu.com/p/a77fd7b09bd1
7.应用层 (多了4个文件,但跟实际使用没有多大关系)
/**
UIImage category for image metadata, including animation, loop count, format, incremental, etc.
*/
用于图像元数据的ui图像类别,包括动画、循环计数、格式、增量等
@interface UIImage (Metadata)
/**
Provide some commen method for `UIImage`.
Image process is based on Core Graphics and vImage.
*/
@interface UIImage (Transform)
为“uiimage”提供一些通用方法,拉伸,裁剪,圆角等等。
图像处理基于Core Graphics 和 vImage
8.引入SDWebImageContext / SDWebImageMutableContext
可以看到SDWebImageContext / SDWebImageMutableContext 其实就是
以 SDWebImageContextOption为key、id(指定类型或者协议)为value 的NSDictionary/NSMutableDictionary
typedef NSDictionary<SDWebImageContextOption, id> SDWebImageContext;
typedef NSMutableDictionary<SDWebImageContextOption, id>SDWebImageMutableContext;
而 SDWebImageContextOption 是一个可扩展的String枚举
typedef NSString * SDWebImageContextOption NS_EXTENSIBLE_STRING_ENUM;
SDWebImage定义了10个SDWebImageContextOption的key、对应的value类型和定义的位置
Key |
Value |
Define |
SDWebImageContextSetImageOperationKey |
NSString |
SDWebImageDefine.m |
SDWebImageContextCustomManager |
SDWebImageManager |
SDWebImageDefine.m |
SDWebImageContextImageTransformer |
id<SDImageTransformer> |
SDWebImageDefine.m |
SDWebImageContextImageScaleFactor |
CGFloat |
SDWebImageDefine.m |
SDWebImageContextStoreCacheType |
SDImageCacheType |
SDWebImageDefine.m |
SDWebImageContextDownloadRequestModifier |
id<SDWebImageDownloaderRequestModifier> |
SDWebImageDefine.m |
SDWebImageContextCacheKeyFilter |
id<SDWebImageCacheKeyFilter> |
SDWebImageDefine.m |
SDWebImageContextCacheSerializer |
id<SDWebImageCacheSerializer> |
SDWebImageDefine.m |
SDWebImageContextLoaderCachedImage |
UIImage/NSImage<SDAnimatedImage> |
SDImageLoader.m |
通过图片看下SDWebImageContext有多重要
You can see from the chart, SDWebImageContext like a "pipeline", the items inside the parameters of View from the outermost layer has been transferred to the SDImageCache and SDWebImageDownloaderOperation. "Pipeline" through the various modules will each get their own interest from up using something (color solid arrow)
Detailed: https://www.jianshu.com/p/cfde8db5c051
9. The agreement of a number of important objects. (flexible)
Before an operation must be done with the object Any object can now comply with the agreement as long as you can
With the newly introduced SDWebImageContext / SDWebImageMutableContext parameters, while retaining the flexibility to quickly use the characteristics of the advanced features of Custom
4.4 |
5.0 |
SDWebImageCacheSerializerBlock |
id<SDWebImageCacheSerializer> |
SDWebImageCacheKeyFilterBlock |
id<SDWebImageCacheKeyFilter> |
SDWebImageDownloader |
id<SDImageLoader> |
SDImageCache |
id<SDImageCache> |
SDWebImageDownloaderProgressBlock |
id<SDWebImageIndicator> |
Detailed: https://www.jianshu.com/p/cfde8db5c051
10.UIImage + GIF method change
Remarks:
1.NS_EXTENSIBLE_STRING_ENUM string type enumeration