Skip to content

Commit c666424

Browse files
author
Olivier Poitrey
committed
Ensure we always set the UIKit image properties from the main thread (fix SDWebImage#403, fix SDWebImage#417, fix SDWebImage#398)
1 parent 9a1f072 commit c666424

File tree

3 files changed

+73
-25
lines changed

3 files changed

+73
-25
lines changed

SDWebImage/MKAnnotationView+WebCache.m

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,27 @@ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder opt
4949
__weak MKAnnotationView *wself = self;
5050
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
5151
{
52-
__strong MKAnnotationView *sself = wself;
53-
if (!sself) return;
54-
if (image)
52+
if (!wself) return;
53+
void (^block)(void) = ^
5554
{
56-
sself.image = image;
55+
__strong MKAnnotationView *sself = wself;
56+
if (!sself) return;
57+
if (image)
58+
{
59+
sself.image = image;
60+
}
61+
if (completedBlock && finished)
62+
{
63+
completedBlock(image, error, cacheType);
64+
}
65+
};
66+
if ([NSThread isMainThread])
67+
{
68+
block();
5769
}
58-
if (completedBlock && finished)
70+
else
5971
{
60-
completedBlock(image, error, cacheType);
72+
dispatch_sync(dispatch_get_main_queue(), block);
6173
}
6274
}];
6375
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

SDWebImage/UIButton+WebCache.m

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,27 @@ - (void)setImageWithURL:(NSURL *)url forState:(UIControlState)state placeholderI
4848
__weak UIButton *wself = self;
4949
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
5050
{
51-
__strong UIButton *sself = wself;
52-
if (!sself) return;
53-
if (image)
51+
if (!wself) return;
52+
void (^block)(void) = ^
5453
{
55-
[sself setImage:image forState:state];
54+
__strong UIButton *sself = wself;
55+
if (!sself) return;
56+
if (image)
57+
{
58+
[sself setImage:image forState:state];
59+
}
60+
if (completedBlock && finished)
61+
{
62+
completedBlock(image, error, cacheType);
63+
}
64+
};
65+
if ([NSThread isMainThread])
66+
{
67+
block();
5668
}
57-
if (completedBlock && finished)
69+
else
5870
{
59-
completedBlock(image, error, cacheType);
71+
dispatch_sync(dispatch_get_main_queue(), block);
6072
}
6173
}];
6274
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
@@ -99,15 +111,27 @@ - (void)setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state pl
99111
__weak UIButton *wself = self;
100112
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
101113
{
102-
__strong UIButton *sself = wself;
103-
if (!sself) return;
104-
if (image)
114+
if (!wself) return;
115+
void (^block)(void) = ^
116+
{
117+
__strong UIButton *sself = wself;
118+
if (!sself) return;
119+
if (image)
120+
{
121+
[sself setBackgroundImage:image forState:state];
122+
}
123+
if (completedBlock && finished)
124+
{
125+
completedBlock(image, error, cacheType);
126+
}
127+
};
128+
if ([NSThread isMainThread])
105129
{
106-
[sself setBackgroundImage:image forState:state];
130+
block();
107131
}
108-
if (completedBlock && finished)
132+
else
109133
{
110-
completedBlock(image, error, cacheType);
134+
dispatch_sync(dispatch_get_main_queue(), block);
111135
}
112136
}];
113137
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

SDWebImage/UIImageView+WebCache.m

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,28 @@ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder opt
5454
__weak UIImageView *wself = self;
5555
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
5656
{
57-
__strong UIImageView *sself = wself;
58-
if (!sself) return;
59-
if (image)
57+
if (!wself) return;
58+
void (^block)(void) = ^
6059
{
61-
sself.image = image;
62-
[sself setNeedsLayout];
60+
__strong UIImageView *sself = wself;
61+
if (!sself) return;
62+
if (image)
63+
{
64+
sself.image = image;
65+
[sself setNeedsLayout];
66+
}
67+
if (completedBlock && finished)
68+
{
69+
completedBlock(image, error, cacheType);
70+
}
71+
};
72+
if ([NSThread isMainThread])
73+
{
74+
block();
6375
}
64-
if (completedBlock && finished)
76+
else
6577
{
66-
completedBlock(image, error, cacheType);
78+
dispatch_sync(dispatch_get_main_queue(), block);
6779
}
6880
}];
6981
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

0 commit comments

Comments
 (0)