Merge pull request #3293 from dreampiggy/bugfix_image_loaders_can_request

Fix the support for `SDImageLoadersManager` for canRequest API check, should be compatible with both old and new API
This commit is contained in:
DreamPiggy 2021-10-19 11:15:56 +08:00 committed by GitHub
commit 602af15947
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View File

@ -78,10 +78,20 @@
#pragma mark - SDImageLoader
- (BOOL)canRequestImageForURL:(nullable NSURL *)url {
return [self canRequestImageForURL:url options:0 context:nil];
}
- (BOOL)canRequestImageForURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context {
NSArray<id<SDImageLoader>> *loaders = self.loaders;
for (id<SDImageLoader> loader in loaders.reverseObjectEnumerator) {
if ([loader canRequestImageForURL:url]) {
return YES;
if ([loader respondsToSelector:@selector(canRequestImageForURL:options:context:)]) {
if ([loader canRequestImageForURL:url options:options context:context]) {
return YES;
}
} else {
if ([loader canRequestImageForURL:url]) {
return YES;
}
}
}
return NO;

View File

@ -774,6 +774,7 @@
SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
expect([loader canRequestImageForURL:imageURL]).beTruthy();
expect([loader canRequestImageForURL:imageURL options:0 context:nil]).beTruthy();
NSError *imageError = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:nil];
expect([loader shouldBlockFailedURLWithURL:imageURL error:imageError]).equal(NO);
@ -797,6 +798,7 @@
manager.loaders = @[SDWebImageDownloader.sharedDownloader, loader];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
expect([manager canRequestImageForURL:imageURL]).beTruthy();
expect([manager canRequestImageForURL:imageURL options:0 context:nil]).beTruthy();
NSError *imageError = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:nil];
expect([manager shouldBlockFailedURLWithURL:imageURL error:imageError]).equal(NO);