你要知道的NSURLSession都在这里
转载请注明出处 http://blog.youkuaiyun.com/u014205968/article/details/78416946
本系列文章主要讲解iOS中网络请求类NSURLSession的使用方法进行详解,同时也会以此为扩展,讲解SDWebImage中图片下载功能的源码分析,讲解AFNetworking相关源码分析。本系列文章主要分为以下几篇进行讲解,读者可按需查阅。
- iOS网络——NSURLSession详解及SDWebImage源码解析
- iOS网络——SDWebImage SDImageDownloader源码解析
- iOS网络——AFNetworking AFURLSessionManager源码解析
- iOS网络——AFNetworking AFHttpSessionManager源码解析
SDWebImage SDWebImageDownloader源码解析
前一篇文章中讲解了SDWebImageDownloaderOperation
是如何自定义NSOperation
子类以及如何使用NSURLSession
实现下载的,本文将会讲解SDWebImageDownloader
类,来探索SDWebImage
如何实现多线程下载多张图片的。
首先看一下接口声明代码:
//下载选项设置的一系列枚举
typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) {
SDWebImageDownloaderLowPriority = 1 << 0,
SDWebImageDownloaderProgressiveDownload = 1 << 1,
SDWebImageDownloaderUseNSURLCache = 1 << 2,
SDWebImageDownloaderIgnoreCachedResponse = 1 << 3,
SDWebImageDownloaderContinueInBackground = 1 << 4,
SDWebImageDownloaderHandleCookies = 1 << 5,
SDWebImageDownloaderAllowInvalidSSLCertificates = 1 << 6,
SDWebImageDownloaderHighPriority = 1 << 7,
SDWebImageDownloaderScaleDownLargeImages = 1 << 8,
};
//下载图片时的顺序,FIFO或者LIFO
typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
SDWebImageDownloaderFIFOExecutionOrder,
SDWebImageDownloaderLIFOExecutionOrder
};
//声明通知的全局变量名
FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadStartNotification;
FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadStopNotification;
//进度回调块
typedef void(^SDWebImageDownloaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL);
//下载完成的回调块
typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished);
//http首部数据字典
typedef NSDictionary<NSString *, NSString *> SDHTTPHeadersDictionary;
//http首部可变数据字典
typedef NSMutableDictionary<NSString *, NSString *> SDHTTPHeadersMutableDictionary;
//http首部过滤的一个回调块
typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterBlock)(NSURL * _Nullable url, SDHTTPHeadersDictionary * _Nullable headers);
上面就是一些枚举、变量、自定义类型的声明。
/*
自定义token类,用于取消下载任务
这个token第二个属性其实就是SDWebImageDownloaderOperation中使用的token即回调块的字典
目的相同,都是为了取消特定的下载任务
*/
@interface SDWebImageDownloadToken : NSObject
@property (nonatomic, strong, nullable) NSURL *url;
@property (nonatomic, strong, nullable) id downloadOperationCancelToken;
@end
//异步下载图片
@interface SDWebImageDownloader : NSObject
//是否压缩图片
@property (assign, nonatomic) BOOL shouldDecompressImages;
//支持的最大同时下载图片的数量,其实就是NSOperationQueue支持的最大并发数
@property (assign, nonatomic) NSInteger maxConcurrentDownloads;
//当前正在下载图片的数量,其实就是NSOperationQueue的operationCount即正在执行下载任务的operation的数量
@property (readonly, nonatomic) NSUInteger currentDownloadCount;
//下载时连接服务器的超时时间,默认15s
@property (assign, nonatomic) NSTimeInterval downloadTimeout;
//session运行模式,默认使用默认模式,即 defaultSessionConfiguration
@property (readonly, nonatomic, nonnull) NSURLSessionConfiguration *sessionConfiguration;
//执行下载任务的顺序,FIFO或LIFO
@property (assign, nonatomic) SDWebImageDownloaderExecutionOrder executionOrder;
//类方法,获取全局共享的单例对象
+ (nonnull instancetype)sharedDownloader;
//默认的URL credential
@property (strong, nonatomic, nullable) NSURLCredential *urlCredential;
//用户名,有些图片下载的地址需要做用户认证
@property (strong, nonatomic, nullable) NSString *username;