|
@@ -10,7 +10,11 @@ |
|
@@ -10,7 +10,11 @@ |
10
|
#import "SDWebImageDownloaderOperation.h"
|
10
|
#import "SDWebImageDownloaderOperation.h"
|
11
|
#import <ImageIO/ImageIO.h>
|
11
|
#import <ImageIO/ImageIO.h>
|
12
|
|
12
|
|
|
|
13
|
+#define AliCloudHttpDNS
|
|
|
14
|
+
|
|
|
15
|
+#ifdef AliCloudHttpDNS
|
13
|
#import <AlicloudHttpDNS/AlicloudHttpDNS.h>
|
16
|
#import <AlicloudHttpDNS/AlicloudHttpDNS.h>
|
|
|
17
|
+#endif
|
14
|
|
18
|
|
15
|
static NSString *const kProgressCallbackKey = @"progress";
|
19
|
static NSString *const kProgressCallbackKey = @"progress";
|
16
|
static NSString *const kCompletedCallbackKey = @"completed";
|
20
|
static NSString *const kCompletedCallbackKey = @"completed";
|
|
@@ -82,7 +86,7 @@ static NSString *const kCompletedCallbackKey = @"completed"; |
|
@@ -82,7 +86,7 @@ static NSString *const kCompletedCallbackKey = @"completed"; |
82
|
#if DEBUG
|
86
|
#if DEBUG
|
83
|
_enableHttpDNS = YES;
|
87
|
_enableHttpDNS = YES;
|
84
|
#else
|
88
|
#else
|
85
|
- _enableHttpDNS = [[[NSUserDefaults standardUserDefaults] stringForKey:@"kUserConfigHttpDNS"] isEqualToString:@"2a90dfa0f37b92aaebf369e9a4d38ba4"];
|
89
|
+ _enableHttpDNS = YES;//[[[NSUserDefaults standardUserDefaults] stringForKey:@"kUserConfigHttpDNS"] isEqualToString:@"2a90dfa0f37b92aaebf369e9a4d38ba4"];
|
86
|
#endif
|
90
|
#endif
|
87
|
}
|
91
|
}
|
88
|
return self;
|
92
|
return self;
|
|
@@ -125,100 +129,101 @@ static NSString *const kCompletedCallbackKey = @"completed"; |
|
@@ -125,100 +129,101 @@ static NSString *const kCompletedCallbackKey = @"completed"; |
125
|
- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock {
|
129
|
- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock {
|
126
|
__block SDWebImageDownloaderOperation *operation;
|
130
|
__block SDWebImageDownloaderOperation *operation;
|
127
|
__weak __typeof(self)wself = self;
|
131
|
__weak __typeof(self)wself = self;
|
128
|
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
129
|
- BOOL useAliDNS = NO;
|
|
|
130
|
- NSString *imageURLHost = url.host;//获取图片URL的host
|
|
|
131
|
- NSURL *newURL = url;
|
|
|
132
|
- if (wself.enableHttpDNS && (imageURLHost.length > 0)) {
|
|
|
133
|
- NSArray *ips = [[HttpDnsService sharedInstance] getIpsByHostAsync:imageURLHost];//同步请求ip地址列表
|
|
|
134
|
- if ([ips isKindOfClass:[NSArray class]] && [ips respondsToSelector:@selector(count)] && ips.count > 0) {
|
|
|
135
|
- NSString *ipAdress = [ips objectAtIndex:0];
|
|
|
136
|
- NSString *urlString = [url absoluteString];
|
|
|
137
|
- NSRange hostRange = [urlString rangeOfString:imageURLHost];
|
|
|
138
|
- if ((NSNotFound != hostRange.location) && (ipAdress.length > 0)) {
|
|
|
139
|
- useAliDNS = YES;
|
|
|
140
|
- NSString *convertedURLString = [urlString stringByReplacingCharactersInRange:hostRange withString:ipAdress];//host转换之后的urlstring
|
|
|
141
|
- newURL = [NSURL URLWithString:convertedURLString]; //替换原始url为转换后的值
|
|
|
142
|
- }
|
|
|
143
|
- }
|
|
|
144
|
- }
|
|
|
145
|
|
132
|
|
146
|
- [wself addProgressCallback:progressBlock andCompletedBlock:completedBlock forURL:url createCallback:^{
|
|
|
147
|
- NSTimeInterval timeoutInterval = wself.downloadTimeout;
|
|
|
148
|
- if (timeoutInterval == 0.0) {
|
|
|
149
|
- timeoutInterval = 15.0;
|
133
|
+ BOOL useAliDNS = NO;
|
|
|
134
|
+ NSString *imageURLHost = url.host;//获取图片URL的host
|
|
|
135
|
+ NSURL *newURL = url;
|
|
|
136
|
+#ifdef AliCloudHttpDNS
|
|
|
137
|
+ if (self.enableHttpDNS && (imageURLHost.length > 0)) {
|
|
|
138
|
+ NSArray *ips = [[HttpDnsService sharedInstance] getIpsByHostAsync:imageURLHost];//同步请求ip地址列表
|
|
|
139
|
+ if ([ips isKindOfClass:[NSArray class]] && [ips respondsToSelector:@selector(count)] && ips.count > 0) {
|
|
|
140
|
+ NSString *ipAdress = [ips objectAtIndex:0];
|
|
|
141
|
+ NSString *urlString = [url absoluteString];
|
|
|
142
|
+ NSRange hostRange = [urlString rangeOfString:imageURLHost];
|
|
|
143
|
+ if ((NSNotFound != hostRange.location) && (ipAdress.length > 0)) {
|
|
|
144
|
+ useAliDNS = YES;
|
|
|
145
|
+ NSString *convertedURLString = [urlString stringByReplacingCharactersInRange:hostRange withString:ipAdress];//host转换之后的urlstring
|
|
|
146
|
+ newURL = [NSURL URLWithString:convertedURLString]; //替换原始url为转换后的值
|
150
|
}
|
147
|
}
|
|
|
148
|
+ }
|
|
|
149
|
+ }
|
|
|
150
|
+#endif
|
|
|
151
|
+
|
|
|
152
|
+ [self addProgressCallback:progressBlock andCompletedBlock:completedBlock forURL:url createCallback:^{
|
|
|
153
|
+ NSTimeInterval timeoutInterval = wself.downloadTimeout;
|
|
|
154
|
+ if (timeoutInterval == 0.0) {
|
|
|
155
|
+ timeoutInterval = 15.0;
|
|
|
156
|
+ }
|
151
|
|
157
|
|
152
|
- // In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests if told otherwise
|
|
|
153
|
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:newURL cachePolicy:(options & SDWebImageDownloaderUseNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:timeoutInterval];
|
|
|
154
|
- request.HTTPShouldHandleCookies = (options & SDWebImageDownloaderHandleCookies);
|
|
|
155
|
- request.HTTPShouldUsePipelining = YES;
|
|
|
156
|
- if (wself.headersFilter) {
|
|
|
157
|
- request.allHTTPHeaderFields = wself.headersFilter(newURL, [wself.HTTPHeaders copy]);
|
|
|
158
|
- }
|
|
|
159
|
- else {
|
|
|
160
|
- request.allHTTPHeaderFields = wself.HTTPHeaders;
|
|
|
161
|
- }
|
|
|
162
|
- if (useAliDNS) { //如果使用了DNS功能,要设置httpheader的host字段的值,否则请求失败
|
|
|
163
|
- [request setValue:imageURLHost forHTTPHeaderField:@"Host"];
|
|
|
164
|
- }
|
|
|
165
|
- operation = [[wself.operationClass alloc] initWithRequest:request
|
|
|
166
|
- options:options
|
|
|
167
|
- progress:^(NSInteger receivedSize, NSInteger expectedSize) {
|
|
|
168
|
- SDWebImageDownloader *sself = wself;
|
|
|
169
|
- if (!sself) return;
|
|
|
170
|
- __block NSArray *callbacksForURL;
|
|
|
171
|
- dispatch_sync(sself.barrierQueue, ^{
|
|
|
172
|
- callbacksForURL = [sself.URLCallbacks[url] copy];
|
|
|
173
|
- });
|
|
|
174
|
- for (NSDictionary *callbacks in callbacksForURL) {
|
|
|
175
|
- SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey];
|
|
|
176
|
- if (callback) callback(receivedSize, expectedSize);
|
|
|
177
|
- }
|
158
|
+ // In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests if told otherwise
|
|
|
159
|
+ NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:newURL cachePolicy:(options & SDWebImageDownloaderUseNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:timeoutInterval];
|
|
|
160
|
+ request.HTTPShouldHandleCookies = (options & SDWebImageDownloaderHandleCookies);
|
|
|
161
|
+ request.HTTPShouldUsePipelining = YES;
|
|
|
162
|
+ if (wself.headersFilter) {
|
|
|
163
|
+ request.allHTTPHeaderFields = wself.headersFilter(newURL, [wself.HTTPHeaders copy]);
|
|
|
164
|
+ }
|
|
|
165
|
+ else {
|
|
|
166
|
+ request.allHTTPHeaderFields = wself.HTTPHeaders;
|
|
|
167
|
+ }
|
|
|
168
|
+ if (useAliDNS) { //如果使用了DNS功能,要设置httpheader的host字段的值,否则请求失败
|
|
|
169
|
+ [request setValue:imageURLHost forHTTPHeaderField:@"Host"];
|
|
|
170
|
+ }
|
|
|
171
|
+ operation = [[wself.operationClass alloc] initWithRequest:request
|
|
|
172
|
+ options:options
|
|
|
173
|
+ progress:^(NSInteger receivedSize, NSInteger expectedSize) {
|
|
|
174
|
+ SDWebImageDownloader *sself = wself;
|
|
|
175
|
+ if (!sself) return;
|
|
|
176
|
+ __block NSArray *callbacksForURL;
|
|
|
177
|
+ dispatch_sync(sself.barrierQueue, ^{
|
|
|
178
|
+ callbacksForURL = [sself.URLCallbacks[url] copy];
|
|
|
179
|
+ });
|
|
|
180
|
+ for (NSDictionary *callbacks in callbacksForURL) {
|
|
|
181
|
+ SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey];
|
|
|
182
|
+ if (callback) callback(receivedSize, expectedSize);
|
178
|
}
|
183
|
}
|
179
|
- completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
|
|
|
180
|
- // NSLog(@"download image completed:url == %@,\nimage = %@",url,image);
|
|
|
181
|
- SDWebImageDownloader *sself = wself;
|
|
|
182
|
- if (!sself) return;
|
|
|
183
|
- __block NSArray *callbacksForURL;
|
|
|
184
|
- dispatch_barrier_sync(sself.barrierQueue, ^{
|
|
|
185
|
- callbacksForURL = [sself.URLCallbacks[url] copy];
|
|
|
186
|
- if (finished) {
|
|
|
187
|
- [sself.URLCallbacks removeObjectForKey:url];
|
|
|
188
|
- }
|
|
|
189
|
- });
|
|
|
190
|
- for (NSDictionary *callbacks in callbacksForURL) {
|
|
|
191
|
- SDWebImageDownloaderCompletedBlock callback = callbacks[kCompletedCallbackKey];
|
|
|
192
|
- if (callback) callback(image, data, error, finished);
|
184
|
+ }
|
|
|
185
|
+ completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
|
|
|
186
|
+// NSLog(@"download image completed:url == %@,\nimage = %@",url,image);
|
|
|
187
|
+ SDWebImageDownloader *sself = wself;
|
|
|
188
|
+ if (!sself) return;
|
|
|
189
|
+ __block NSArray *callbacksForURL;
|
|
|
190
|
+ dispatch_barrier_sync(sself.barrierQueue, ^{
|
|
|
191
|
+ callbacksForURL = [sself.URLCallbacks[url] copy];
|
|
|
192
|
+ if (finished) {
|
|
|
193
|
+ [sself.URLCallbacks removeObjectForKey:url];
|
193
|
}
|
194
|
}
|
|
|
195
|
+ });
|
|
|
196
|
+ for (NSDictionary *callbacks in callbacksForURL) {
|
|
|
197
|
+ SDWebImageDownloaderCompletedBlock callback = callbacks[kCompletedCallbackKey];
|
|
|
198
|
+ if (callback) callback(image, data, error, finished);
|
194
|
}
|
199
|
}
|
195
|
- cancelled:^{
|
|
|
196
|
- SDWebImageDownloader *sself = wself;
|
|
|
197
|
- if (!sself) return;
|
|
|
198
|
- dispatch_barrier_async(sself.barrierQueue, ^{
|
|
|
199
|
- [sself.URLCallbacks removeObjectForKey:url];
|
|
|
200
|
- });
|
|
|
201
|
- }];
|
|
|
202
|
- operation.shouldDecompressImages = wself.shouldDecompressImages;
|
|
|
203
|
-
|
|
|
204
|
- if (wself.username && wself.password) {
|
|
|
205
|
- operation.credential = [NSURLCredential credentialWithUser:wself.username password:wself.password persistence:NSURLCredentialPersistenceForSession];
|
|
|
206
|
- }
|
|
|
207
|
-
|
|
|
208
|
- if (options & SDWebImageDownloaderHighPriority) {
|
|
|
209
|
- operation.queuePriority = NSOperationQueuePriorityHigh;
|
|
|
210
|
- } else if (options & SDWebImageDownloaderLowPriority) {
|
|
|
211
|
- operation.queuePriority = NSOperationQueuePriorityLow;
|
|
|
212
|
- }
|
200
|
+ }
|
|
|
201
|
+ cancelled:^{
|
|
|
202
|
+ SDWebImageDownloader *sself = wself;
|
|
|
203
|
+ if (!sself) return;
|
|
|
204
|
+ dispatch_barrier_async(sself.barrierQueue, ^{
|
|
|
205
|
+ [sself.URLCallbacks removeObjectForKey:url];
|
|
|
206
|
+ });
|
|
|
207
|
+ }];
|
|
|
208
|
+ operation.shouldDecompressImages = wself.shouldDecompressImages;
|
|
|
209
|
+
|
|
|
210
|
+ if (wself.username && wself.password) {
|
|
|
211
|
+ operation.credential = [NSURLCredential credentialWithUser:wself.username password:wself.password persistence:NSURLCredentialPersistenceForSession];
|
|
|
212
|
+ }
|
|
|
213
|
+
|
|
|
214
|
+ if (options & SDWebImageDownloaderHighPriority) {
|
|
|
215
|
+ operation.queuePriority = NSOperationQueuePriorityHigh;
|
|
|
216
|
+ } else if (options & SDWebImageDownloaderLowPriority) {
|
|
|
217
|
+ operation.queuePriority = NSOperationQueuePriorityLow;
|
|
|
218
|
+ }
|
213
|
|
219
|
|
214
|
- [wself.downloadQueue addOperation:operation];
|
|
|
215
|
- if (wself.executionOrder == SDWebImageDownloaderLIFOExecutionOrder) {
|
|
|
216
|
- // Emulate LIFO execution order by systematically adding new operations as last operation's dependency
|
|
|
217
|
- [wself.lastAddedOperation addDependency:operation];
|
|
|
218
|
- wself.lastAddedOperation = operation;
|
|
|
219
|
- }
|
|
|
220
|
- }];
|
|
|
221
|
- });
|
220
|
+ [wself.downloadQueue addOperation:operation];
|
|
|
221
|
+ if (wself.executionOrder == SDWebImageDownloaderLIFOExecutionOrder) {
|
|
|
222
|
+ // Emulate LIFO execution order by systematically adding new operations as last operation's dependency
|
|
|
223
|
+ [wself.lastAddedOperation addDependency:operation];
|
|
|
224
|
+ wself.lastAddedOperation = operation;
|
|
|
225
|
+ }
|
|
|
226
|
+ }];
|
222
|
|
227
|
|
223
|
return operation;
|
228
|
return operation;
|
224
|
}
|
229
|
}
|