From c6664248f1530fd1624aae4da823fe0374f6e26f Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Wed, 12 Jun 2013 17:01:01 +0200 Subject: [PATCH] Ensure we always set the UIKit image properties from the main thread (fix #403, fix #417, fix #398) --- SDWebImage/MKAnnotationView+WebCache.m | 24 +++++++++---- SDWebImage/UIButton+WebCache.m | 48 +++++++++++++++++++------- SDWebImage/UIImageView+WebCache.m | 26 ++++++++++---- 3 files changed, 73 insertions(+), 25 deletions(-) diff --git a/SDWebImage/MKAnnotationView+WebCache.m b/SDWebImage/MKAnnotationView+WebCache.m index 8f27a176..4485fb2c 100644 --- a/SDWebImage/MKAnnotationView+WebCache.m +++ b/SDWebImage/MKAnnotationView+WebCache.m @@ -49,15 +49,27 @@ static char operationKey; __weak MKAnnotationView *wself = self; id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { - __strong MKAnnotationView *sself = wself; - if (!sself) return; - if (image) + if (!wself) return; + void (^block)(void) = ^ { - sself.image = image; + __strong MKAnnotationView *sself = wself; + if (!sself) return; + if (image) + { + sself.image = image; + } + if (completedBlock && finished) + { + completedBlock(image, error, cacheType); + } + }; + if ([NSThread isMainThread]) + { + block(); } - if (completedBlock && finished) + else { - completedBlock(image, error, cacheType); + dispatch_sync(dispatch_get_main_queue(), block); } }]; objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index 53c99a5e..ba1734dc 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -48,15 +48,27 @@ static char operationKey; __weak UIButton *wself = self; id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { - __strong UIButton *sself = wself; - if (!sself) return; - if (image) + if (!wself) return; + void (^block)(void) = ^ { - [sself setImage:image forState:state]; + __strong UIButton *sself = wself; + if (!sself) return; + if (image) + { + [sself setImage:image forState:state]; + } + if (completedBlock && finished) + { + completedBlock(image, error, cacheType); + } + }; + if ([NSThread isMainThread]) + { + block(); } - if (completedBlock && finished) + else { - completedBlock(image, error, cacheType); + dispatch_sync(dispatch_get_main_queue(), block); } }]; objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); @@ -99,15 +111,27 @@ static char operationKey; __weak UIButton *wself = self; id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { - __strong UIButton *sself = wself; - if (!sself) return; - if (image) + if (!wself) return; + void (^block)(void) = ^ { - [sself setBackgroundImage:image forState:state]; + __strong UIButton *sself = wself; + if (!sself) return; + if (image) + { + [sself setBackgroundImage:image forState:state]; + } + if (completedBlock && finished) + { + completedBlock(image, error, cacheType); + } + }; + if ([NSThread isMainThread]) + { + block(); } - if (completedBlock && finished) + else { - completedBlock(image, error, cacheType); + 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 01eab545..2bfc31ac 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -54,16 +54,28 @@ static char operationKey; __weak UIImageView *wself = self; id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { - __strong UIImageView *sself = wself; - if (!sself) return; - if (image) + if (!wself) return; + void (^block)(void) = ^ { - sself.image = image; - [sself setNeedsLayout]; + __strong UIImageView *sself = wself; + if (!sself) return; + if (image) + { + sself.image = image; + [sself setNeedsLayout]; + } + if (completedBlock && finished) + { + completedBlock(image, error, cacheType); + } + }; + if ([NSThread isMainThread]) + { + block(); } - if (completedBlock && finished) + else { - completedBlock(image, error, cacheType); + dispatch_sync(dispatch_get_main_queue(), block); } }]; objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);