Skip to content

Commit af3e4f8

Browse files
committed
Returning error in setImage completedBlock if the url was nil. Added dispatch_main_async_safe macro. Fixes #505
1 parent eb91fdd commit af3e4f8

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

SDWebImage/MKAnnotationView+WebCache.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
6060
});
6161
}];
6262
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
63+
} else {
64+
dispatch_main_async_safe(^{
65+
NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
66+
if (completedBlock) {
67+
completedBlock(nil, error, SDImageCacheTypeNone, url);
68+
}
69+
});
6370
}
6471
}
6572

SDWebImage/SDWebImageCompat.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ extern UIImage *SDScaledImageForKey(NSString *key, UIImage *image);
5656
#define dispatch_main_sync_safe(block)\
5757
if ([NSThread isMainThread]) {\
5858
block();\
59-
}\
60-
else {\
59+
} else {\
6160
dispatch_sync(dispatch_get_main_queue(), block);\
6261
}
62+
63+
#define dispatch_main_async_safe(block)\
64+
if ([NSThread isMainThread]) {\
65+
block();\
66+
} else {\
67+
dispatch_async(dispatch_get_main_queue(), block);\
68+
}

SDWebImage/UIButton+WebCache.m

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ @implementation UIButton (WebCache)
1717
- (NSURL *)currentImageURL {
1818
NSURL *url = self.imageURLStorage[@(self.state)];
1919

20-
if (!url)
21-
{
20+
if (!url) {
2221
url = self.imageURLStorage[@(UIControlStateNormal)];
2322
}
2423

@@ -57,6 +56,13 @@ - (void)sd_setImageWithURL:(NSURL *)url forState:(UIControlState)state placehold
5756
if (!url) {
5857
[self.imageURLStorage removeObjectForKey:@(state)];
5958

59+
dispatch_main_async_safe(^{
60+
NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
61+
if (completedBlock) {
62+
completedBlock(nil, error, SDImageCacheTypeNone, url);
63+
}
64+
});
65+
6066
return;
6167
}
6268

@@ -120,6 +126,13 @@ - (void)sd_setBackgroundImageWithURL:(NSURL *)url forState:(UIControlState)state
120126
});
121127
}];
122128
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
129+
} else {
130+
dispatch_main_async_safe(^{
131+
NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
132+
if (completedBlock) {
133+
completedBlock(nil, error, SDImageCacheTypeNone, url);
134+
}
135+
});
123136
}
124137
}
125138

SDWebImage/UIImageView+HighlightedWebCache.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ - (void)sd_setHighlightedImageWithURL:(NSURL *)url options:(SDWebImageOptions)op
4949
});
5050
}];
5151
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
52+
} else {
53+
dispatch_main_async_safe(^{
54+
NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
55+
if (completedBlock) {
56+
completedBlock(nil, error, SDImageCacheTypeNone, url);
57+
}
58+
});
5259
}
5360
}
5461

SDWebImage/UIImageView+WebCache.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
6969
});
7070
}];
7171
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
72+
} else {
73+
dispatch_main_async_safe(^{
74+
NSError *error = [NSError errorWithDomain:@"SDWebImageErrorDomain" code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
75+
if (completedBlock) {
76+
completedBlock(nil, error, SDImageCacheTypeNone, url);
77+
}
78+
});
7279
}
7380
}
7481

0 commit comments

Comments
 (0)