|
@@ -20,6 +20,7 @@ static NSString *const kCompletedCallbackKey = @"completed"; |
|
@@ -20,6 +20,7 @@ static NSString *const kCompletedCallbackKey = @"completed"; |
20
|
|
20
|
|
21
|
@property (strong, nonatomic) NSOperationQueue *downloadQueue;
|
21
|
@property (strong, nonatomic) NSOperationQueue *downloadQueue;
|
22
|
@property (weak, nonatomic) NSOperation *lastAddedOperation;
|
22
|
@property (weak, nonatomic) NSOperation *lastAddedOperation;
|
|
|
23
|
+@property (assign, nonatomic) Class operationClass;
|
23
|
@property (strong, nonatomic) NSMutableDictionary *URLCallbacks;
|
24
|
@property (strong, nonatomic) NSMutableDictionary *URLCallbacks;
|
24
|
@property (strong, nonatomic) NSMutableDictionary *HTTPHeaders;
|
25
|
@property (strong, nonatomic) NSMutableDictionary *HTTPHeaders;
|
25
|
// This queue is used to serialize the handling of the network responses of all the download operation in a single queue
|
26
|
// This queue is used to serialize the handling of the network responses of all the download operation in a single queue
|
|
@@ -63,6 +64,7 @@ static NSString *const kCompletedCallbackKey = @"completed"; |
|
@@ -63,6 +64,7 @@ static NSString *const kCompletedCallbackKey = @"completed"; |
63
|
|
64
|
|
64
|
- (id)init {
|
65
|
- (id)init {
|
65
|
if ((self = [super init])) {
|
66
|
if ((self = [super init])) {
|
|
|
67
|
+ _operationClass = [SDWebImageDownloaderOperation class];
|
66
|
_executionOrder = SDWebImageDownloaderFIFOExecutionOrder;
|
68
|
_executionOrder = SDWebImageDownloaderFIFOExecutionOrder;
|
67
|
_downloadQueue = [NSOperationQueue new];
|
69
|
_downloadQueue = [NSOperationQueue new];
|
68
|
_downloadQueue.maxConcurrentOperationCount = 2;
|
70
|
_downloadQueue.maxConcurrentOperationCount = 2;
|
|
@@ -104,6 +106,10 @@ static NSString *const kCompletedCallbackKey = @"completed"; |
|
@@ -104,6 +106,10 @@ static NSString *const kCompletedCallbackKey = @"completed"; |
104
|
return _downloadQueue.maxConcurrentOperationCount;
|
106
|
return _downloadQueue.maxConcurrentOperationCount;
|
105
|
}
|
107
|
}
|
106
|
|
108
|
|
|
|
109
|
+- (void)setOperationClass:(Class)operationClass {
|
|
|
110
|
+ _operationClass = operationClass ?: [SDWebImageDownloaderOperation class];
|
|
|
111
|
+}
|
|
|
112
|
+
|
107
|
- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock {
|
113
|
- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock {
|
108
|
__block SDWebImageDownloaderOperation *operation;
|
114
|
__block SDWebImageDownloaderOperation *operation;
|
109
|
__weak SDWebImageDownloader *wself = self;
|
115
|
__weak SDWebImageDownloader *wself = self;
|
|
@@ -124,34 +130,34 @@ static NSString *const kCompletedCallbackKey = @"completed"; |
|
@@ -124,34 +130,34 @@ static NSString *const kCompletedCallbackKey = @"completed"; |
124
|
else {
|
130
|
else {
|
125
|
request.allHTTPHeaderFields = wself.HTTPHeaders;
|
131
|
request.allHTTPHeaderFields = wself.HTTPHeaders;
|
126
|
}
|
132
|
}
|
127
|
- operation = [[SDWebImageDownloaderOperation alloc] initWithRequest:request
|
|
|
128
|
- options:options
|
|
|
129
|
- progress:^(NSInteger receivedSize, NSInteger expectedSize) {
|
|
|
130
|
- SDWebImageDownloader *sself = wself;
|
|
|
131
|
- if (!sself) return;
|
|
|
132
|
- NSArray *callbacksForURL = [sself callbacksForURL:url];
|
|
|
133
|
- for (NSDictionary *callbacks in callbacksForURL) {
|
|
|
134
|
- SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey];
|
|
|
135
|
- if (callback) callback(receivedSize, expectedSize);
|
|
|
136
|
- }
|
|
|
137
|
- }
|
|
|
138
|
- completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
|
|
|
139
|
- SDWebImageDownloader *sself = wself;
|
|
|
140
|
- if (!sself) return;
|
|
|
141
|
- NSArray *callbacksForURL = [sself callbacksForURL:url];
|
|
|
142
|
- if (finished) {
|
|
|
143
|
- [sself removeCallbacksForURL:url];
|
|
|
144
|
- }
|
|
|
145
|
- for (NSDictionary *callbacks in callbacksForURL) {
|
|
|
146
|
- SDWebImageDownloaderCompletedBlock callback = callbacks[kCompletedCallbackKey];
|
|
|
147
|
- if (callback) callback(image, data, error, finished);
|
|
|
148
|
- }
|
|
|
149
|
- }
|
|
|
150
|
- cancelled:^{
|
|
|
151
|
- SDWebImageDownloader *sself = wself;
|
|
|
152
|
- if (!sself) return;
|
|
|
153
|
- [sself removeCallbacksForURL:url];
|
|
|
154
|
- }];
|
133
|
+ operation = [[wself.operationClass alloc] initWithRequest:request
|
|
|
134
|
+ options:options
|
|
|
135
|
+ progress:^(NSInteger receivedSize, NSInteger expectedSize) {
|
|
|
136
|
+ SDWebImageDownloader *sself = wself;
|
|
|
137
|
+ if (!sself) return;
|
|
|
138
|
+ NSArray *callbacksForURL = [sself callbacksForURL:url];
|
|
|
139
|
+ for (NSDictionary *callbacks in callbacksForURL) {
|
|
|
140
|
+ SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey];
|
|
|
141
|
+ if (callback) callback(receivedSize, expectedSize);
|
|
|
142
|
+ }
|
|
|
143
|
+ }
|
|
|
144
|
+ completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
|
|
|
145
|
+ SDWebImageDownloader *sself = wself;
|
|
|
146
|
+ if (!sself) return;
|
|
|
147
|
+ NSArray *callbacksForURL = [sself callbacksForURL:url];
|
|
|
148
|
+ if (finished) {
|
|
|
149
|
+ [sself removeCallbacksForURL:url];
|
|
|
150
|
+ }
|
|
|
151
|
+ for (NSDictionary *callbacks in callbacksForURL) {
|
|
|
152
|
+ SDWebImageDownloaderCompletedBlock callback = callbacks[kCompletedCallbackKey];
|
|
|
153
|
+ if (callback) callback(image, data, error, finished);
|
|
|
154
|
+ }
|
|
|
155
|
+ }
|
|
|
156
|
+ cancelled:^{
|
|
|
157
|
+ SDWebImageDownloader *sself = wself;
|
|
|
158
|
+ if (!sself) return;
|
|
|
159
|
+ [sself removeCallbacksForURL:url];
|
|
|
160
|
+ }];
|
155
|
|
161
|
|
156
|
if (wself.username && wself.password) {
|
162
|
if (wself.username && wself.password) {
|
157
|
operation.credential = [NSURLCredential credentialWithUser:wself.username password:wself.password persistence:NSURLCredentialPersistenceForSession];
|
163
|
operation.credential = [NSURLCredential credentialWithUser:wself.username password:wself.password persistence:NSURLCredentialPersistenceForSession];
|