iOS开发之SDWebImage源码查看摘要

SDWebImage源码查看流程

UIImageView+WebCache

两个方法


//从网络获取图片并展示  创建一个下载操作,然后把这个操作设置为view的一个属性,然后为UIImageView添加一个url的属性,如果下载成功显示图片
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;



//从缓存中读取图片、不走网络请求(从缓存中获取图片,有就展示没有展示占位图片)
- (void)sd_setImageWithPreviousCachedImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;

//将下载的图片数组动态播放
- (void)sd_setAnimationImagesWithURLs:(NSArray *)arrayOfURLs

UIView+WebCacheOperation

//当创建一个下载操作的时候,吧对应的操作存储为当前view的一个属性(方便控制取消或者停止等操作)
- (void)sd_setImageLoadOperation:(id)operation forKey:(NSString *)key;

//取消对应的操作
- (void)sd_cancelImageLoadOperationWithKey:(NSString *)key;

//删除对应key的下载操作
- (void)sd_removeImageLoadOperationWithKey:(NSString *)key; 


UIButton+WebCache

1、会通过运行时为Button添加一个 属性
imageURLStorageKey:{state(buttonzhuangtai):对应的url}
2、在为UIView添加操作属性时,
UIButtonImageOperation+state : operation
UIButtonBackgroundImageOperation+state: Operation

//下载对应状态下的图片
- (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock;
//下载对应状态下的背景图片
- (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletionBlock)completedBlock;
//取消对应状态下的图片下载
- (void)sd_cancelImageLoadForState:(UIControlState)state;

//取消对应状态下的背景图片下载
- (void)sd_cancelBackgroundImageLoadForState:(UIControlState)state;

SDImageManager

1、SDWebImageOptions下载选项
2、SDWebImageManagerDelegate代理
3、属性

    1.delegate(代理)
    2.SDImageCache *imageCache(缓存)
    3.SDWebImageDownloader  *imageDownloader(下载器啊)
    4.SDWebImageCacheKeyFilterBlock cacheKeyFilter(存储的图片key的生成规则)

4、API:

1. 下载图片:(id )downloadImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionWithFinishedBlock)completedBlock;
2. 存储图片:- (void)saveImageToCache:(UIImage *)image forURL:(NSURL *)url;
3. 查询图片是否存在API

SDWebImageDownloader

头文件

1、下载选项枚举
2、下载顺序枚举
3、下载开始结束通知
4、下载进度、下载完成、request header生成规则block
5、属性

1.是否在下载后、缓存后解压图片
2.最大并发数
3.当前下载数
4.下载超时时间
5.下载顺序

6、设置http header的属性和方法
7、下载图片API

(id )downloadImageWithURL:(NSURL *)url
                                     options:(SDWebImageDownloaderOptions)options
                                    progress:(SDWebImageDownloaderProgressBlock)progressBlock
                                   completed:(SDWebImageDownloaderCompletedBlock)completedBlock;

8、下载队列挂起API,取消所有下载操作API

源文件

1、属性

1.下载队列NSOperationQueue *downloadQueue
2.最后一个下载操作:NSOperation *lastAddedOperation
3.图片下载回调数组 :NSMutableDictionary *URLCallbacks
4.图片请求头:NSMutableDictionary *HTTPHeaders
5.网络请求响应处理队列: dispatch_queue_t barrierQueue;
6.当前网络会话:NSURLSession *session

2、主要是下载图片API:

SDWebImageDownloaderOperation

头文件

1、网络请求的通知

    extern NSString *const SDWebImageDownloadStartNotification;
    extern NSString *const SDWebImageDownloadReceiveResponseNotification;
    extern NSString *const SDWebImageDownloadStopNotification;
    extern NSString *const SDWebImageDownloadFinishNotification;

2、网络请求对象 :NSURLRequest *request
3、请求任务:NSURLSessionTask *dataTask
4、下载完是否解压图片:BOOL shouldDecompressImages
5、url认证:NSURLCredential *credential
6、下载选项:SDWebImageDownloaderOptions options
7、请求响应:NSURLResponse *response

8、初始化方法:

    (id)initWithRequest:(NSURLRequest *)request
        inSession:(NSURLSession *)session
          options:(SDWebImageDownloaderOptions)options
         progress:(SDWebImageDownloaderProgressBlock)progressBlock
        completed:(SDWebImageDownloaderCompletedBlock)completedBlock
        cancelled:(SDWebImageNoParamsBlock)cancelBlock;

源文件

1、初始化函数,初始化属性
2、start函数(任务加到队列中自动调用)
3、取消任务API
4、NSURLSessionDataDelegate 回调函数

SDImageCache

头文件

1、缓存方式枚举
2、查询block,检查是否在缓存block,计算文件个数和大小block
3、属性

    1、是否解压图片
    2、是否禁用iCould
    3、是在在内存中缓存图片
    4、最大内存存储量
    5、最大内存存储图片数量
    6、最大缓存时间
    7、最大缓存大小

4、初始化API
5、存储图片API
6、查询API
7、清除API
8、获取缓存路径API

源文件

1、初始化API

    initWithNamespace:(NSString *)ns diskCacheDirectory:(NSString *)directory

2、获取图片缓存路径API

    cachePathForKey:(NSString *)key inPath:(NSString *)path

3、存储图片API:

    storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk

4、查询图片API:

    queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompletedBlock)doneBlock

图片分类

1、UIImage+GIF:解析gif
2、UIImage+WebP:解析webp(需要通过cocodpods导入libwebp库)
3、UIImage+MultiFormat: 统一图片解析入口,内部根据图片类型调用不同的解析方法
4、NSData+ImageContentType: 获取图片data对应的类型信息(PNG、JPEG)

其它

1、SDWebImageCompat :定义了一些“宏”,还有一个缩放图片函数
2、SDWebImageDecoder:图片解码
3、SDWebImageOperation: 定义了一个协议,有一个cancel方法

你可能感兴趣的:(iOS开发之SDWebImage源码查看摘要)