SDWebImage源代码阅读(四)
2016-09-18 22:59
99 查看
SDWebImage源代码阅读(四)
SDWebImage源代码阅读(一)——SDWebImage的使用和UIImageView+WebCacheSDWebImage源代码阅读(二)——SDWebImageManager
SDWebImage源代码阅读(三)——SDCache
SDWebImageDownloader
+ (SDWebImageDownloader *)sharedDownloader { static dispatch_once_t once; static id instance; dispatch_once(&once, ^{ instance = [self new]; }); return instance; }
单例
初始化
- (id)init { if ((self = [super init])) { _operationClass = [SDWebImageDownloaderOperation class]; _shouldDecompressImages = YES; _executionOrder = SDWebImageDownloaderFIFOExecutionOrder; _downloadQueue = [NSOperationQueue new]; _downloadQueue.maxConcurrentOperationCount = 6; _downloadQueue.name = @"com.hackemist.SDWebImageDownloader"; _URLCallbacks = [NSMutableDictionary new]; #ifdef SD_WEBP _HTTPHeaders = [@{@"Accept": @"image/webp,image/*;q=0.8"} mutableCopy]; #else _HTTPHeaders = [@{@"Accept": @"image/*;q=0.8"} mutableCopy]; #endif _barrierQueue = dispatch_queue_create("com.hackemist.SDWebImageDownloaderBarrierQueue", DISPATCH_QUEUE_CONCURRENT); _downloadTimeout = 15.0; NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; sessionConfig.timeoutIntervalForRequest = _downloadTimeout; /** * Create the session for this task * We send nil as delegate queue so that the session creates a serial operation queue for performing all delegate * method calls and completion handler calls. */ self.session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil]; } return self; }
maxConcurrentOperationCount对你最多并发数,默认为6
_downloadTimeout = 15.0下载超时的时间
下载图片
- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock { ··· }
其中执行
[self addProgressCallback:progressBlock completedBlock:completedBlock forURL:url createCallback:^{···}使用
dispatch_barrier_sync保证只有一个队列执行,并且给URL添加回调函数,并且处理同一个网址下载的同一图片,保证每个URL都执行一次
createCallback();
在
createCallback()我们会新创建一个操作队列,来执行下载操作
[wself.downloadQueue addOperation:operation];就是将操作放入下载队列中
typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) { /** * Default value. All download operations will execute in queue style (first-in-first-out). */ SDWebImageDownloaderFIFOExecutionOrder, /** * All download operations will execute in stack style (last-in-first-out). */ SDWebImageDownloaderLIFOExecutionOrder };
SDWebImageDownloader还提供了两种下载任务调度方式(先进先出和后进先出)
SDWebImageDownloaderOperation
初始化方法,在SDWebImageDownloader中调用:- (id)initWithRequest:(NSURLRequest *)request inSession:(NSURLSession *)session options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock cancelled:(SDWebImageNoParamsBlock)cancelBlock { if ((self = [super init])) { _request = [request copy]; _shouldDecompressImages = YES; _options = options; _progressBlock = [progressBlock copy]; _completedBlock = [completedBlock copy]; _cancelBlock = [cancelBlock copy]; _executing = NO; _finished = NO; _expectedSize = 0; _unownedSession = session; responseFromCached = YES; // Initially wrong until `- URLSession:dataTask:willCacheResponse:completionHandler: is called or not called } return self; }
SDWebImageDownloaderOperation使用
start和
done来控制状态,而不是使用main。图片的下载使用
NSURLConnection,在协议中接收数据并回调Block通知下载进度和下载完成。
相关文章推荐
- SDWebImage源代码阅读(二)
- SDWebImage源代码阅读(三)
- SDWebImage源代码阅读(一)
- 如何阅读一本书?
- 关于ipad阅读某些pdf文件出现乱码的简单解决办法
- Hi3531添加16GByte(128Gbit) NAND Flash支持 分类: HI3531 2013-08-28 10:23 861人阅读 评论(0) 收藏
- Real-World Concurrency阅读笔记
- CABasicAnimation 基本动画 分类: ios技术 2015-07-16 17:10 132人阅读 评论(0) 收藏
- 阅读笔记05
- Paper阅读小结
- 阅读《Visual C# 基于组件的开发》有感
- <<重构手册>>阅读笔记:重构的含义
- 如何阅读mysql源码
- 第十四周项目6-阅读程序1(对continue的理解)
- leetCode(40):Path Sum 分类: leetCode 2015-07-17 16:46 125人阅读 评论(0) 收藏
- 《机器学习(周志华)》 阅读笔记
- caffe代码阅读10:Caffe中卷积的实现细节(涉及到BaseConvolutionLayer、ConvolutionLayer、im2col等)-2016.4.3
- C++ FAQ阅读笔记[1]
- 存储器的寻址问题 分类: 计算机组成原理 2011-04-04 23:24 477人阅读 评论(0) 收藏
- HDOJ 4240 - Route Redundancy 最大流+DP...阅读理解...