diff --git a/SDWebImage/MKAnnotationView+WebCache.m b/SDWebImage/MKAnnotationView+WebCache.m index 4485fb2cf..82dc5b9e2 100644 --- a/SDWebImage/MKAnnotationView+WebCache.m +++ b/SDWebImage/MKAnnotationView+WebCache.m @@ -50,7 +50,7 @@ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder opt id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { if (!wself) return; - void (^block)(void) = ^ + dispatch_main_sync_safe(^ { __strong MKAnnotationView *sself = wself; if (!sself) return; @@ -62,15 +62,7 @@ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder opt { completedBlock(image, error, cacheType); } - }; - if ([NSThread isMainThread]) - { - block(); - } - else - { - dispatch_sync(dispatch_get_main_queue(), block); - } + }); }]; objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 620ba0954..4450e2596 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -280,7 +280,7 @@ - (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDIm [self.memCache setObject:diskImage forKey:key cost:cost]; } - dispatch_async(dispatch_get_main_queue(), ^ + dispatch_main_sync_safe(^ { doneBlock(diskImage, SDImageCacheTypeDisk); }); @@ -479,7 +479,7 @@ - (void)calculateSizeWithCompletionBlock:(void (^)(NSUInteger fileCount, unsigne if (completionBlock) { - dispatch_async(dispatch_get_main_queue(), ^ + dispatch_main_sync_safe(^ { completionBlock(fileCount, totalSize); }); diff --git a/SDWebImage/SDWebImageCompat.h b/SDWebImage/SDWebImageCompat.h index 4815e4e2c..673af06cb 100644 --- a/SDWebImage/SDWebImageCompat.h +++ b/SDWebImage/SDWebImageCompat.h @@ -41,4 +41,14 @@ #define SDDispatchQueueSetterSementics assign #endif -extern UIImage *SDScaledImageForKey(NSString *key, UIImage *image); +extern inline UIImage *SDScaledImageForKey(NSString *key, UIImage *image); + +#define dispatch_main_sync_safe(block)\ + if ([NSThread isMainThread])\ + {\ + block();\ + }\ + else\ + {\ + dispatch_sync(dispatch_get_main_queue(), block);\ + } diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index df394ab7a..6c7650278 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -230,7 +230,7 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data UIImage *scaledImage = [self scaledImageForKey:self.request.URL.absoluteString image:image]; image = [UIImage decodedImageWithImage:scaledImage]; CGImageRelease(partialImageRef); - dispatch_async(dispatch_get_main_queue(), ^ + dispatch_main_sync_safe(^ { if (self.completedBlock) { diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index ebe945dcb..9c680d96d 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -163,7 +163,7 @@ - (NSString *)cacheKeyForURL:(NSURL *)url { UIImage *transformedImage = [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url]; - dispatch_async(dispatch_get_main_queue(), ^ + dispatch_main_sync_safe(^ { completedBlock(transformedImage, nil, SDImageCacheTypeNone, finished); }); diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index ba1734dc7..3b8420e9b 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -49,7 +49,7 @@ - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderI id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { if (!wself) return; - void (^block)(void) = ^ + dispatch_main_sync_safe(^ { __strong UIButton *sself = wself; if (!sself) return; @@ -61,15 +61,7 @@ - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderI { completedBlock(image, error, cacheType); } - }; - if ([NSThread isMainThread]) - { - block(); - } - else - { - dispatch_sync(dispatch_get_main_queue(), block); - } + }); }]; objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @@ -112,7 +104,7 @@ - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state pl id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { if (!wself) return; - void (^block)(void) = ^ + dispatch_main_sync_safe(^ { __strong UIButton *sself = wself; if (!sself) return; @@ -124,15 +116,7 @@ - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state pl { completedBlock(image, error, cacheType); } - }; - if ([NSThread isMainThread]) - { - block(); - } - else - { - dispatch_sync(dispatch_get_main_queue(), block); - } + }); }]; objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index dd9bfcbb0..fa772236e 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -56,7 +56,7 @@ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder opt id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { if (!wself) return; - void (^block)(void) = ^ + dispatch_main_sync_safe(^ { __strong UIImageView *sself = wself; if (!sself) return; @@ -69,58 +69,47 @@ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder opt { completedBlock(image, error, cacheType); } - }; - if ([NSThread isMainThread]) - { - block(); - } - else - { - dispatch_sync(dispatch_get_main_queue(), block); - } + }); }]; objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } } --(void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs{ - [self cancelCurrentArrayLoad]; - __weak UIImageView *wself = self; - - NSMutableArray *operationsArray = [[NSMutableArray alloc] init]; - - for(NSURL *logoImageURL in arrayOfURLs){ - id operation = [SDWebImageManager.sharedManager downloadWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished){ - if (!wself) return; - void (^block)(void) = ^ - { - __strong UIImageView *sself = wself; - [sself stopAnimating]; - if (!sself) return; - if (image) - { - NSMutableArray *currentImages = [[sself animationImages] mutableCopy]; - if(!currentImages) currentImages = [[NSMutableArray alloc] init]; - [currentImages addObject:image]; - - sself.animationImages = currentImages; - [sself setNeedsLayout]; - } - [sself startAnimating]; - }; - if ([NSThread isMainThread]) - { - block(); - } - else - { - dispatch_sync(dispatch_get_main_queue(), block); - } - }]; - [operationsArray addObject:operation]; - } - - objc_setAssociatedObject(self, &operationArrayKey, [NSArray arrayWithArray:operationsArray], OBJC_ASSOCIATION_RETAIN_NONATOMIC); +- (void)setAnimationImagesWithURLs:(NSArray *)arrayOfURLs +{ + [self cancelCurrentArrayLoad]; + __weak UIImageView *wself = self; + + NSMutableArray *operationsArray = [[NSMutableArray alloc] init]; + + for (NSURL *logoImageURL in arrayOfURLs) + { + id operation = [SDWebImageManager.sharedManager downloadWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) + { + if (!wself) return; + dispatch_main_sync_safe(^ + { + __strong UIImageView *sself = wself; + [sself stopAnimating]; + if (sself && image) + { + NSMutableArray *currentImages = [[sself animationImages] mutableCopy]; + if (!currentImages) + { + currentImages = [[NSMutableArray alloc] init]; + } + [currentImages addObject:image]; + + sself.animationImages = currentImages; + [sself setNeedsLayout]; + } + [sself startAnimating]; + }); + }]; + [operationsArray addObject:operation]; + } + + objc_setAssociatedObject(self, &operationArrayKey, [NSArray arrayWithArray:operationsArray], OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (void)cancelCurrentImageLoad @@ -136,16 +125,16 @@ - (void)cancelCurrentImageLoad - (void)cancelCurrentArrayLoad { - // Cancel in progress downloader from queue - NSArray *operations = objc_getAssociatedObject(self, &operationArrayKey); - for(id operation in operations){ - if (operation) - { - [operation cancel]; - } - } - objc_setAssociatedObject(self, &operationArrayKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); - + // Cancel in progress downloader from queue + NSArray *operations = objc_getAssociatedObject(self, &operationArrayKey); + for (id operation in operations) + { + if (operation) + { + [operation cancel]; + } + } + objc_setAssociatedObject(self, &operationArrayKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @end