Authored by 朱小军

修改原先子线程查询图片URL的方式,子线程在suboperation的外部执行 review by 于良

... ... @@ -10,7 +10,11 @@
#import "SDWebImageDownloaderOperation.h"
#import <ImageIO/ImageIO.h>
#define AliCloudHttpDNS
#ifdef AliCloudHttpDNS
#import <AlicloudHttpDNS/AlicloudHttpDNS.h>
#endif
static NSString *const kProgressCallbackKey = @"progress";
static NSString *const kCompletedCallbackKey = @"completed";
... ... @@ -82,7 +86,7 @@ static NSString *const kCompletedCallbackKey = @"completed";
#if DEBUG
_enableHttpDNS = YES;
#else
_enableHttpDNS = [[[NSUserDefaults standardUserDefaults] stringForKey:@"kUserConfigHttpDNS"] isEqualToString:@"2a90dfa0f37b92aaebf369e9a4d38ba4"];
_enableHttpDNS = YES;//[[[NSUserDefaults standardUserDefaults] stringForKey:@"kUserConfigHttpDNS"] isEqualToString:@"2a90dfa0f37b92aaebf369e9a4d38ba4"];
#endif
}
return self;
... ... @@ -125,11 +129,12 @@ static NSString *const kCompletedCallbackKey = @"completed";
- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock {
__block SDWebImageDownloaderOperation *operation;
__weak __typeof(self)wself = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
BOOL useAliDNS = NO;
NSString *imageURLHost = url.host;//获取图片URL的host
NSURL *newURL = url;
if (wself.enableHttpDNS && (imageURLHost.length > 0)) {
#ifdef AliCloudHttpDNS
if (self.enableHttpDNS && (imageURLHost.length > 0)) {
NSArray *ips = [[HttpDnsService sharedInstance] getIpsByHostAsync:imageURLHost];//同步请求ip地址列表
if ([ips isKindOfClass:[NSArray class]] && [ips respondsToSelector:@selector(count)] && ips.count > 0) {
NSString *ipAdress = [ips objectAtIndex:0];
... ... @@ -142,8 +147,9 @@ static NSString *const kCompletedCallbackKey = @"completed";
}
}
}
#endif
[wself addProgressCallback:progressBlock andCompletedBlock:completedBlock forURL:url createCallback:^{
[self addProgressCallback:progressBlock andCompletedBlock:completedBlock forURL:url createCallback:^{
NSTimeInterval timeoutInterval = wself.downloadTimeout;
if (timeoutInterval == 0.0) {
timeoutInterval = 15.0;
... ... @@ -177,7 +183,7 @@ static NSString *const kCompletedCallbackKey = @"completed";
}
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
// NSLog(@"download image completed:url == %@,\nimage = %@",url,image);
// NSLog(@"download image completed:url == %@,\nimage = %@",url,image);
SDWebImageDownloader *sself = wself;
if (!sself) return;
__block NSArray *callbacksForURL;
... ... @@ -218,7 +224,6 @@ static NSString *const kCompletedCallbackKey = @"completed";
wself.lastAddedOperation = operation;
}
}];
});
return operation;
}
... ...
... ... @@ -180,15 +180,20 @@
// ignore image read from NSURLCache if image if cached but force refreshing
downloaderOptions |= SDWebImageDownloaderIgnoreCachedResponse;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
id <SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) {
if (weakOperation.isCancelled) {
SDWebImageCombinedOperation *ssOperation = weakOperation;
if (ssOperation.isCancelled) {
// Do nothing if the operation was cancelled
// See #699 for more details
// if we would call the completedBlock, there could be a race condition between this block and another completedBlock for the same object, so if this one is called second, we will overwrite the new data
}
else if (error) {
dispatch_main_sync_safe(^{
if (!weakOperation.isCancelled) {
if (!ssOperation.isCancelled) {
completedBlock(nil, error, SDImageCacheTypeNone, finished, url);
}
});
... ... @@ -223,7 +228,7 @@
}
dispatch_main_sync_safe(^{
if (!weakOperation.isCancelled) {
if (!ssOperation.isCancelled) {
completedBlock(transformedImage, nil, SDImageCacheTypeNone, finished, url);
}
});
... ... @@ -235,7 +240,7 @@
}
dispatch_main_sync_safe(^{
if (!weakOperation.isCancelled) {
if (!ssOperation.isCancelled) {
completedBlock(downloadedImage, nil, SDImageCacheTypeNone, finished, url);
}
});
... ... @@ -255,6 +260,8 @@
[self.runningOperations removeObject:weakOperation];
}
};
});
}
else if (image) {
dispatch_main_sync_safe(^{
... ...