Rename the SDImageLoaderProtocol method naming to avoid the Selector conflict with SDWebImageManager and cause misunderstanding usage

This commit is contained in:
DreamPiggy 2019-03-25 18:44:41 +08:00
parent 6621f6a37d
commit 9fe1eee005
6 changed files with 22 additions and 22 deletions

View File

@ -26,7 +26,7 @@ FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextLoader
/**
This is the built-in decoding process for image download from network or local file.
@note If you want to implement your custom loader with `loadImageWithURL:options:context:progress:completed:` API, but also want to keep compatible with SDWebImage's behavior, you'd better use this to produce image.
@note If you want to implement your custom loader with `requestImageWithURL:options:context:progress:completed:` API, but also want to keep compatible with SDWebImage's behavior, you'd better use this to produce image.
@param imageData The image data from the network. Should not be nil
@param imageURL The image URL from the input. Should not be nil
@ -38,7 +38,7 @@ FOUNDATION_EXPORT UIImage * _Nullable SDImageLoaderDecodeImageData(NSData * _Non
/**
This is the built-in decoding process for image progressive download from network. It's used when `SDWebImageProgressiveLoad` option is set. (It's not required when your loader does not support progressive image loading)
@note If you want to implement your custom loader with `loadImageWithURL:options:context:progress:completed:` API, but also want to keep compatible with SDWebImage's behavior, you'd better use this to produce image.
@note If you want to implement your custom loader with `requestImageWithURL:options:context:progress:completed:` API, but also want to keep compatible with SDWebImage's behavior, you'd better use this to produce image.
@param imageData The image data from the network so far. Should not be nil
@param imageURL The image URL from the input. Should not be nil
@ -61,12 +61,12 @@ FOUNDATION_EXPORT UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NS
/**
Whether current image loader supports to load the provide image URL.
This will be checked everytime a new image request come for loader. If this return NO, we will mark this image load as failed. If return YES, we will start to call `loadImageWithURL:options:context:progress:completed:`.
This will be checked everytime a new image request come for loader. If this return NO, we will mark this image load as failed. If return YES, we will start to call `requestImageWithURL:options:context:progress:completed:`.
@param url The image URL to be loaded.
@return YES to continue download, NO to stop download.
*/
- (BOOL)canLoadWithURL:(nullable NSURL *)url;
- (BOOL)canRequestImageForURL:(nullable NSURL *)url;
/**
Load the image and image data with the given URL and return the image data. You're responsible for producing the image instance.
@ -79,10 +79,10 @@ FOUNDATION_EXPORT UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NS
@param completedBlock A block called when operation has been completed.
@return An operation which allow the user to cancel the current request.
*/
- (nullable id<SDWebImageOperation>)loadImageWithURL:(nullable NSURL *)url
options:(SDWebImageOptions)options
context:(nullable SDWebImageContext *)context
progress:(nullable SDImageLoaderProgressBlock)progressBlock
completed:(nullable SDImageLoaderCompletedBlock)completedBlock;
- (nullable id<SDWebImageOperation>)requestImageWithURL:(nullable NSURL *)url
options:(SDWebImageOptions)options
context:(nullable SDWebImageContext *)context
progress:(nullable SDImageLoaderProgressBlock)progressBlock
completed:(nullable SDImageLoaderCompletedBlock)completedBlock;
@end

View File

@ -65,19 +65,19 @@
#pragma mark - SDImageLoader
- (BOOL)canLoadWithURL:(nullable NSURL *)url {
- (BOOL)canRequestImageForURL:(nullable NSURL *)url {
SD_LOCK(self.loadersLock);
NSArray<id<SDImageLoader>> *loaders = self.loaders;
SD_UNLOCK(self.loadersLock);
for (id<SDImageLoader> loader in loaders.reverseObjectEnumerator) {
if ([loader canLoadWithURL:url]) {
if ([loader canRequestImageForURL:url]) {
return YES;
}
}
return NO;
}
- (id<SDWebImageOperation>)loadImageWithURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDImageLoaderCompletedBlock)completedBlock {
- (id<SDWebImageOperation>)requestImageWithURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDImageLoaderCompletedBlock)completedBlock {
if (!url) {
return nil;
}
@ -85,8 +85,8 @@
NSArray<id<SDImageLoader>> *loaders = self.loaders;
SD_UNLOCK(self.loadersLock);
for (id<SDImageLoader> loader in loaders.reverseObjectEnumerator) {
if ([loader canLoadWithURL:url]) {
return [loader loadImageWithURL:url options:options context:context progress:progressBlock completed:completedBlock];
if ([loader canRequestImageForURL:url]) {
return [loader requestImageWithURL:url options:options context:context progress:progressBlock completed:completedBlock];
}
}
return nil;

View File

@ -499,7 +499,7 @@ didReceiveResponse:(NSURLResponse *)response
@implementation SDWebImageDownloader (SDImageLoader)
- (BOOL)canLoadWithURL:(NSURL *)url {
- (BOOL)canRequestImageForURL:(NSURL *)url {
if (!url) {
return NO;
}
@ -507,7 +507,7 @@ didReceiveResponse:(NSURLResponse *)response
return YES;
}
- (id<SDWebImageOperation>)loadImageWithURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDImageLoaderCompletedBlock)completedBlock {
- (id<SDWebImageOperation>)requestImageWithURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDImageLoaderCompletedBlock)completedBlock {
UIImage *cachedImage = context[SDWebImageContextLoaderCachedImage];
SDWebImageDownloaderOptions downloaderOptions = 0;

View File

@ -218,7 +218,7 @@ static id<SDImageLoader> _defaultImageLoader;
BOOL shouldDownload = (options & SDWebImageFromCacheOnly) == 0;
shouldDownload &= (!cachedImage || options & SDWebImageRefreshCached);
shouldDownload &= (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url]);
shouldDownload &= [self.imageLoader canLoadWithURL:url];
shouldDownload &= [self.imageLoader canRequestImageForURL:url];
if (shouldDownload) {
if (cachedImage && options & SDWebImageRefreshCached) {
// If image was found in the cache but SDWebImageRefreshCached is provided, notify about the cached image
@ -237,7 +237,7 @@ static id<SDImageLoader> _defaultImageLoader;
// `SDWebImageCombinedOperation` -> `SDWebImageDownloadToken` -> `downloadOperationCancelToken`, which is a `SDCallbacksDictionary` and retain the completed block below, so we need weak-strong again to avoid retain cycle
@weakify(operation);
operation.loaderOperation = [self.imageLoader loadImageWithURL:url options:options context:context progress:progressBlock completed:^(UIImage *downloadedImage, NSData *downloadedData, NSError *error, BOOL finished) {
operation.loaderOperation = [self.imageLoader requestImageWithURL:url options:options context:context progress:progressBlock completed:^(UIImage *downloadedImage, NSData *downloadedData, NSError *error, BOOL finished) {
@strongify(operation);
if (!operation || operation.isCancelled) {
// Do nothing if the operation was cancelled

View File

@ -443,7 +443,7 @@
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom image not works"];
SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
[loader loadImageWithURL:imageURL options:0 context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
[loader requestImageWithURL:imageURL options:0 context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
expect(targetURL).notTo.beNil();
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
expect(error).to.beNil();
@ -457,7 +457,7 @@
- (void)test31ThatLoadersManagerWorks {
XCTestExpectation *expectation = [self expectationWithDescription:@"Loaders manager not works"];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
[[SDImageLoadersManager sharedManager] loadImageWithURL:imageURL options:0 context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
[[SDImageLoadersManager sharedManager] requestImageWithURL:imageURL options:0 context:nil progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
expect(targetURL).notTo.beNil();
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
expect(error).to.beNil();

View File

@ -16,11 +16,11 @@
@implementation SDWebImageTestLoader
- (BOOL)canLoadWithURL:(NSURL *)url {
- (BOOL)canRequestImageForURL:(NSURL *)url {
return YES;
}
- (id<SDWebImageOperation>)loadImageWithURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDImageLoaderCompletedBlock)completedBlock {
- (id<SDWebImageOperation>)requestImageWithURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDImageLoaderCompletedBlock)completedBlock {
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {