Merge pull request #2650 from dreampiggy/refactory_image_loader_protocol_naming

Rename the SDImageLoaderProtocol method naming to avoid the Selector conflict with SDWebImageManager and cause misunderstanding usage
This commit is contained in:
Kinarobin 2019-03-27 19:52:01 +08:00 committed by GitHub
commit a00565181a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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. 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 imageData The image data from the network. Should not be nil
@param imageURL The image URL from the input. 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) 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 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 @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. 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. @param url The image URL to be loaded.
@return YES to continue download, NO to stop download. @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. 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. @param completedBlock A block called when operation has been completed.
@return An operation which allow the user to cancel the current request. @return An operation which allow the user to cancel the current request.
*/ */
- (nullable id<SDWebImageOperation>)loadImageWithURL:(nullable NSURL *)url - (nullable id<SDWebImageOperation>)requestImageWithURL:(nullable NSURL *)url
options:(SDWebImageOptions)options options:(SDWebImageOptions)options
context:(nullable SDWebImageContext *)context context:(nullable SDWebImageContext *)context
progress:(nullable SDImageLoaderProgressBlock)progressBlock progress:(nullable SDImageLoaderProgressBlock)progressBlock
completed:(nullable SDImageLoaderCompletedBlock)completedBlock; completed:(nullable SDImageLoaderCompletedBlock)completedBlock;
@end @end

View File

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

View File

@ -499,7 +499,7 @@ didReceiveResponse:(NSURLResponse *)response
@implementation SDWebImageDownloader (SDImageLoader) @implementation SDWebImageDownloader (SDImageLoader)
- (BOOL)canLoadWithURL:(NSURL *)url { - (BOOL)canRequestImageForURL:(NSURL *)url {
if (!url) { if (!url) {
return NO; return NO;
} }
@ -507,7 +507,7 @@ didReceiveResponse:(NSURLResponse *)response
return YES; 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]; UIImage *cachedImage = context[SDWebImageContextLoaderCachedImage];
SDWebImageDownloaderOptions downloaderOptions = 0; SDWebImageDownloaderOptions downloaderOptions = 0;

View File

@ -218,7 +218,7 @@ static id<SDImageLoader> _defaultImageLoader;
BOOL shouldDownload = (options & SDWebImageFromCacheOnly) == 0; BOOL shouldDownload = (options & SDWebImageFromCacheOnly) == 0;
shouldDownload &= (!cachedImage || options & SDWebImageRefreshCached); shouldDownload &= (!cachedImage || options & SDWebImageRefreshCached);
shouldDownload &= (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url]); 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 (shouldDownload) {
if (cachedImage && options & SDWebImageRefreshCached) { if (cachedImage && options & SDWebImageRefreshCached) {
// If image was found in the cache but SDWebImageRefreshCached is provided, notify about the cached image // 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 // `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); @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); @strongify(operation);
if (!operation || operation.isCancelled) { if (!operation || operation.isCancelled) {
// Do nothing if the operation was cancelled // Do nothing if the operation was cancelled

View File

@ -443,7 +443,7 @@
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom image not works"]; XCTestExpectation *expectation = [self expectationWithDescription:@"Custom image not works"];
SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init]; SDWebImageTestLoader *loader = [[SDWebImageTestLoader alloc] init];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL]; 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(); expect(targetURL).notTo.beNil();
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
expect(error).to.beNil(); expect(error).to.beNil();
@ -457,7 +457,7 @@
- (void)test31ThatLoadersManagerWorks { - (void)test31ThatLoadersManagerWorks {
XCTestExpectation *expectation = [self expectationWithDescription:@"Loaders manager not works"]; XCTestExpectation *expectation = [self expectationWithDescription:@"Loaders manager not works"];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL]; 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(); expect(targetURL).notTo.beNil();
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) { } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
expect(error).to.beNil(); expect(error).to.beNil();

View File

@ -16,11 +16,11 @@
@implementation SDWebImageTestLoader @implementation SDWebImageTestLoader
- (BOOL)canLoadWithURL:(NSURL *)url { - (BOOL)canRequestImageForURL:(NSURL *)url {
return YES; 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]; NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {