Refactor to only use one publish protocol for custom loader. Renaming image downloader description to loader
This commit is contained in:
parent
8292c0c1e9
commit
60759f812a
|
@ -248,8 +248,9 @@ typedef SDWebImageLoaderCompletedBlock SDWebImageDownloaderCompletedBlock;
|
|||
|
||||
|
||||
/**
|
||||
SDWebImageDownloader is the built-in image downloader conform to `SDWebImageLoader`. Which provide the HTTP/HTTPS/FTP download, or local file URL using NSURLSession.
|
||||
However, this downloader does also support customization for advanced users. You can specify `operationClass` in download config to custom download operation, See `SDWebImageDownloaderOperation`.
|
||||
SDWebImageDownloader is the built-in image loader conform to `SDWebImageLoader`. Which provide the HTTP/HTTPS/FTP download, or local file URL using NSURLSession.
|
||||
However, this downloader class itself also support customization for advanced users. You can specify `operationClass` in download config to custom download operation, See `SDWebImageDownloaderOperation`.
|
||||
If you want to provide some image loader which beyond network or local file, consider to create your own custom class conform to `SDWebImageLoader`.
|
||||
*/
|
||||
@interface SDWebImageDownloader (SDWebImageLoader) <SDWebImageLoader>
|
||||
|
||||
|
|
|
@ -350,11 +350,15 @@ didReceiveResponse:(NSURLResponse *)response
|
|||
break;
|
||||
}
|
||||
}
|
||||
// If we can't find any progressive coder, disable progressive download
|
||||
if (!self.progressiveCoder) {
|
||||
self.options &= ~SDWebImageDownloaderProgressiveDownload;
|
||||
}
|
||||
}
|
||||
|
||||
// progressive decode the image in coder queue
|
||||
dispatch_async(self.coderQueue, ^{
|
||||
UIImage *image = SDWebImageLoaderDecodeProgressiveImageData(data, self.request.URL, finished, self.progressiveCoder, [[self class] imageOptionsFromDownloaderOptions:self.options], [[self class] imageContextFromDownloadContext:self.context]);
|
||||
UIImage *image = SDWebImageLoaderDecodeProgressiveImageData(data, self.request.URL, finished, self.progressiveCoder, [[self class] imageOptionsFromDownloaderOptions:self.options], self.context);
|
||||
if (image) {
|
||||
// We do not keep the progressive decoding image even when `finished`=YES. Because they are for view rendering but not take full function from downloader options. And some coders implementation may not keep consistent between progressive decoding and normal decoding.
|
||||
|
||||
|
@ -419,7 +423,7 @@ didReceiveResponse:(NSURLResponse *)response
|
|||
} else {
|
||||
// decode the image in coder queue
|
||||
dispatch_async(self.coderQueue, ^{
|
||||
UIImage *image = SDWebImageLoaderDecodeImageData(imageData, self.request.URL, nil, [[self class] imageOptionsFromDownloaderOptions:self.options], [[self class] imageContextFromDownloadContext:self.context]);
|
||||
UIImage *image = SDWebImageLoaderDecodeImageData(imageData, self.request.URL, [[self class] imageOptionsFromDownloaderOptions:self.options], self.context);
|
||||
CGSize imageSize = image.size;
|
||||
if (imageSize.width == 0 || imageSize.height == 0) {
|
||||
[self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}]];
|
||||
|
@ -475,19 +479,11 @@ didReceiveResponse:(NSURLResponse *)response
|
|||
if (downloadOptions & SDWebImageDownloaderScaleDownLargeImages) options |= SDWebImageScaleDownLargeImages;
|
||||
if (downloadOptions & SDWebImageDownloaderDecodeFirstFrameOnly) options |= SDWebImageDecodeFirstFrameOnly;
|
||||
if (downloadOptions & SDWebImageDownloaderPreloadAllFrames) options |= SDWebImagePreloadAllFrames;
|
||||
if (downloadOptions & SDWebImageDownloaderAvoidDecodeImage) options |= SDWebImageAvoidDecodeImage;
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
+ (SDWebImageContext *)imageContextFromDownloadContext:(SDWebImageContext *)downloadContext {
|
||||
SDWebImageContext *context = nil;
|
||||
if (!downloadContext) {
|
||||
return context;
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
- (BOOL)shouldContinueWhenAppEntersBackground {
|
||||
return self.options & SDWebImageDownloaderContinueInBackground;
|
||||
}
|
||||
|
|
|
@ -10,11 +10,19 @@
|
|||
#import "SDWebImageDefine.h"
|
||||
#import "SDWebImageOperation.h"
|
||||
|
||||
@protocol SDWebImageProgressiveCoder;
|
||||
typedef void(^SDWebImageLoaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL);
|
||||
typedef void(^SDWebImageLoaderCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished);
|
||||
typedef void(^SDWebImageLoaderDataCompletedBlock)(NSData * _Nullable data, NSError * _Nullable error, BOOL finished);
|
||||
|
||||
@protocol SDWebImageCoder, SDWebImageProgressiveCoder;
|
||||
#pragma mark - Context
|
||||
|
||||
/**
|
||||
A `UIImage` instance from `SDWebImageManager` when you specify `SDWebImageRefreshCached` and image cache hit.
|
||||
This can be a hint for image loader to load the image from network and refresh the image from remote location if needed. If the cached image is equal to the remote location one. you should call the completion with all nil args. (UIImage)
|
||||
*/
|
||||
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextLoaderCachedImage;
|
||||
|
||||
#pragma mark - Helper method
|
||||
|
||||
/**
|
||||
This is the built-in decoding process for image download from network or local file.
|
||||
|
@ -22,15 +30,14 @@ typedef void(^SDWebImageLoaderDataCompletedBlock)(NSData * _Nullable data, NSErr
|
|||
|
||||
@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 coder The image coder. You can pass nil to use the default `SDWebImageCodersManager`. See `SDWebImageCoder`
|
||||
@param options The options arg from the input
|
||||
@param context The context arg from the input
|
||||
@return The decoded image for current image data load from the network
|
||||
*/
|
||||
FOUNDATION_EXPORT UIImage * _Nullable SDWebImageLoaderDecodeImageData(NSData * _Nonnull imageData, NSURL * _Nonnull imageURL, id<SDWebImageCoder> _Nullable coder, SDWebImageOptions options, SDWebImageContext * _Nullable context);
|
||||
FOUNDATION_EXPORT UIImage * _Nullable SDWebImageLoaderDecodeImageData(NSData * _Nonnull imageData, NSURL * _Nonnull imageURL, SDWebImageOptions options, SDWebImageContext * _Nullable context);
|
||||
|
||||
/**
|
||||
This is the built-in decoding process for image progressive download from network. It's used when `SDWebImageProgressiveDownload` option is set.
|
||||
This is the built-in decoding process for image progressive download from network. It's used when `SDWebImageProgressiveDownload` 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.
|
||||
|
||||
@param imageData The image data from the network so far. Should not be nil
|
||||
|
@ -43,25 +50,24 @@ FOUNDATION_EXPORT UIImage * _Nullable SDWebImageLoaderDecodeImageData(NSData * _
|
|||
*/
|
||||
FOUNDATION_EXPORT UIImage * _Nullable SDWebImageLoaderDecodeProgressiveImageData(NSData * _Nonnull imageData, NSURL * _Nonnull imageURL, BOOL finished, id<SDWebImageProgressiveCoder> _Nonnull progressiveCoder, SDWebImageOptions options, SDWebImageContext * _Nullable context);
|
||||
|
||||
/**
|
||||
A `UIImage` instance from `SDWebImageManager` when you specify `SDWebImageRefreshCached` and image cache hit.
|
||||
This can be a hint for image loader to load the image from network and refresh the image from remote location if needed. If the cached image is equal to the remote location one. you should call the completion with all nil args. (UIImage)
|
||||
*/
|
||||
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextLoaderCachedImage;
|
||||
|
||||
#pragma mark - SDWebImageLoader
|
||||
|
||||
// This is the protocol to specify custom image load process. You can create your own class to conform this protocol and use as a image loader to load image from network or any avaiable remote resources defined by yourself.
|
||||
// If you want to implement custom loader for image download from network or local file, you just need to concentrate on image data download only. After the download finish, call `SDWebImageLoaderDecodeImageData` or `SDWebImageLoaderDecodeProgressiveImageData` to use the built-in decoding process and produce image (Remember to call in the global queue). And finally callback the completion block.
|
||||
// If you directlly get the image instance using some third-party SDKs, such as image directlly from Photos framework. You can process the image data and image instance by yourself without that built-in decoding process. And finally callback the completion block.
|
||||
// @note It's your responsibility to load the image in the desired global queue(to avoid block main queue). We do not dispatch these method call in a global queue but just from the call queue (For `SDWebImageManager`, it typically call from the main queue).
|
||||
|
||||
@protocol SDWebImageLoader <NSObject>
|
||||
|
||||
/**
|
||||
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:`.
|
||||
|
||||
@param url The image URL to be loaded.
|
||||
@return YES to continue download, NO to stop download.
|
||||
*/
|
||||
- (BOOL)canLoadWithURL:(nullable NSURL *)url;
|
||||
|
||||
// We provide two ways to allow a image loader to load the image.
|
||||
// The first one should return the `UIImage` image instance as well as `NSData` image data. This is suitable for the use case such as image from third-party SDKs, such as image directlly from Photos framework.
|
||||
// The second one should return just the `NSData` image data, we will use the common image decoding logic to process the correct image instance, so the image loader itself can concentrate on only data retriving. This is suitable for the use case such as load the data from network or local file.
|
||||
// Your image loader **MUST** implement at least one of those protocol, or an assert will occur. We will firstlly ask for `loadImageWithURL:options:progress:completed:context:` if you implement it. If this one return nil, we will continue to ask for `loadImageDataWithURL:options:progress:completed:context:` if you implement it.
|
||||
// @note It's your responsibility to load the image in the desired global queue(to avoid block main queue). We do not dispatch these method call in a global queue but just from the call queue (For `SDWebImageManager`, it typically call from the main queue).
|
||||
|
||||
@optional
|
||||
/**
|
||||
Load the image and image data with the given URL and return the image data. You're responsible for producing the image instance.
|
||||
|
||||
|
@ -79,21 +85,4 @@ FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextLoader
|
|||
progress:(nullable SDWebImageLoaderProgressBlock)progressBlock
|
||||
completed:(nullable SDWebImageLoaderCompletedBlock)completedBlock;
|
||||
|
||||
/**
|
||||
Load the image with the given URL and return the image data. We will automatically handler the image decoding stuff for you.
|
||||
|
||||
@param url The URL represent the image. Note this may not be a HTTP URL
|
||||
@param options A mask to specify options to use for this request
|
||||
@param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
|
||||
@param progressBlock A block called while image is downloading
|
||||
* @note the progress block is executed on a background queue
|
||||
@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>)loadImageDataWithURL:(nullable NSURL *)url
|
||||
options:(SDWebImageOptions)options
|
||||
context:(nullable SDWebImageContext *)context
|
||||
progress:(nullable SDWebImageLoaderProgressBlock)progressBlock
|
||||
completed:(nullable SDWebImageLoaderDataCompletedBlock)completedBlock;
|
||||
|
||||
@end
|
||||
|
|
|
@ -7,27 +7,30 @@
|
|||
*/
|
||||
|
||||
#import "SDWebImageLoader.h"
|
||||
#import "SDWebImageCacheKeyFilter.h"
|
||||
#import "SDWebImageCodersManager.h"
|
||||
#import "SDWebImageCoderHelper.h"
|
||||
#import "SDAnimatedImage.h"
|
||||
#import "UIImage+WebCache.h"
|
||||
|
||||
UIImage * _Nullable SDWebImageLoaderDecodeImageData(NSData * _Nonnull imageData, NSURL * _Nonnull imageURL, id<SDWebImageCoder> _Nullable coder, SDWebImageOptions options, SDWebImageContext * _Nullable context) {
|
||||
UIImage * _Nullable SDWebImageLoaderDecodeImageData(NSData * _Nonnull imageData, NSURL * _Nonnull imageURL, SDWebImageOptions options, SDWebImageContext * _Nullable context) {
|
||||
NSCParameterAssert(imageData);
|
||||
NSCParameterAssert(imageURL);
|
||||
|
||||
UIImage *image;
|
||||
NSString *cacheKey = imageURL.absoluteString;
|
||||
id<SDWebImageCacheKeyFilter> cacheKeyFilter = [context valueForKey:SDWebImageContextCacheKeyFilter];
|
||||
NSString *cacheKey;
|
||||
if (cacheKeyFilter) {
|
||||
cacheKey = [cacheKeyFilter cacheKeyForURL:imageURL];
|
||||
} else {
|
||||
cacheKey = imageURL.absoluteString;
|
||||
}
|
||||
BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly;
|
||||
NSNumber *scaleValue = [context valueForKey:SDWebImageContextImageScaleFactor];
|
||||
CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey);
|
||||
if (scale < 1) {
|
||||
scale = 1;
|
||||
}
|
||||
if (!coder) {
|
||||
coder = [SDWebImageCodersManager sharedManager];
|
||||
}
|
||||
|
||||
if (!decodeFirstFrame) {
|
||||
// check whether we should use `SDAnimatedImage`
|
||||
if ([context valueForKey:SDWebImageContextAnimatedImageClass]) {
|
||||
|
@ -41,10 +44,10 @@ UIImage * _Nullable SDWebImageLoaderDecodeImageData(NSData * _Nonnull imageData,
|
|||
}
|
||||
}
|
||||
if (!image) {
|
||||
image = [coder decodedImageWithData:imageData options:@{SDWebImageCoderDecodeFirstFrameOnly : @(decodeFirstFrame), SDWebImageCoderDecodeScaleFactor : @(scale)}];
|
||||
image = [[SDWebImageCodersManager sharedManager] decodedImageWithData:imageData options:@{SDWebImageCoderDecodeFirstFrameOnly : @(decodeFirstFrame), SDWebImageCoderDecodeScaleFactor : @(scale)}];
|
||||
}
|
||||
if (image) {
|
||||
BOOL shouldDecode = YES;
|
||||
BOOL shouldDecode = (options & SDWebImageAvoidDecodeImage) == 0;
|
||||
if ([image conformsToProtocol:@protocol(SDAnimatedImage)]) {
|
||||
// `SDAnimatedImage` do not decode
|
||||
shouldDecode = NO;
|
||||
|
@ -72,7 +75,13 @@ UIImage * _Nullable SDWebImageLoaderDecodeProgressiveImageData(NSData * _Nonnull
|
|||
NSCParameterAssert(progressiveCoder);
|
||||
|
||||
UIImage *image;
|
||||
NSString *cacheKey = imageURL.absoluteString;
|
||||
id<SDWebImageCacheKeyFilter> cacheKeyFilter = [context valueForKey:SDWebImageContextCacheKeyFilter];
|
||||
NSString *cacheKey;
|
||||
if (cacheKeyFilter) {
|
||||
cacheKey = [cacheKeyFilter cacheKeyForURL:imageURL];
|
||||
} else {
|
||||
cacheKey = imageURL.absoluteString;
|
||||
}
|
||||
BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly;
|
||||
NSNumber *scaleValue = [context valueForKey:SDWebImageContextImageScaleFactor];
|
||||
CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey);
|
||||
|
@ -94,7 +103,7 @@ UIImage * _Nullable SDWebImageLoaderDecodeProgressiveImageData(NSData * _Nonnull
|
|||
image = [progressiveCoder incrementalDecodedImageWithOptions:@{SDWebImageCoderDecodeFirstFrameOnly : @(decodeFirstFrame), SDWebImageCoderDecodeScaleFactor : @(scale)}];
|
||||
}
|
||||
if (image) {
|
||||
BOOL shouldDecode = YES;
|
||||
BOOL shouldDecode = (options & SDWebImageAvoidDecodeImage) == 0;
|
||||
if ([image conformsToProtocol:@protocol(SDAnimatedImage)]) {
|
||||
// `SDAnimatedImage` do not decode
|
||||
shouldDecode = NO;
|
||||
|
|
|
@ -95,18 +95,4 @@
|
|||
return nil;
|
||||
}
|
||||
|
||||
- (id<SDWebImageOperation>)loadImageDataWithURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDWebImageLoaderProgressBlock)progressBlock completed:(SDWebImageLoaderDataCompletedBlock)completedBlock {
|
||||
if (!url) {
|
||||
return nil;
|
||||
}
|
||||
for (id<SDWebImageLoader> loader in self.loaders) {
|
||||
if ([loader respondsToSelector:@selector(loadImageDataWithURL:options:context:progress:completed:)]) {
|
||||
if ([loader canLoadWithURL:url]) {
|
||||
return [loader loadImageDataWithURL:url options:options context:context progress:progressBlock completed:completedBlock];
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
@ -103,10 +103,9 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager];
|
|||
@property (strong, nonatomic, readonly, nonnull) id<SDWebImageCache> imageCache;
|
||||
|
||||
/**
|
||||
* The image downloader used by manager to download image.
|
||||
* @note If you specify a non-shared downloader, don't forget to call `invalidateSessionAndCancel:` at proper time to avoid memory leak.
|
||||
* The image loader used by manager to load image.
|
||||
*/
|
||||
@property (strong, nonatomic, readonly, nonnull) id<SDWebImageLoader> imageDownloader;
|
||||
@property (strong, nonatomic, readonly, nonnull) id<SDWebImageLoader> imageLoader;
|
||||
|
||||
/**
|
||||
The image transformer for manager. It's used for image transform after the image load finished and store the transformed image to cache, see `SDWebImageTransformer`.
|
||||
|
@ -162,10 +161,10 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager];
|
|||
@property (nonatomic, class, nullable) id<SDWebImageCache> defaultImageCache;
|
||||
|
||||
/**
|
||||
The default image downloader for manager which is created with no arguments. Such as shared manager or init.
|
||||
The default image loader for manager which is created with no arguments. Such as shared manager or init.
|
||||
Defaults to nil. Means using `SDWebImageDownloader.sharedDownloader`
|
||||
*/
|
||||
@property (nonatomic, class, nullable) SDWebImageDownloader *defaultImageDownloader;
|
||||
@property (nonatomic, class, nullable) id<SDWebImageLoader> defaultImageLoader;
|
||||
|
||||
/**
|
||||
* Returns global shared manager instance.
|
||||
|
@ -173,10 +172,10 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager];
|
|||
@property (nonatomic, class, readonly, nonnull) SDWebImageManager *sharedManager;
|
||||
|
||||
/**
|
||||
* Allows to specify instance of cache and image downloader used with image manager.
|
||||
* @return new instance of `SDWebImageManager` with specified cache and downloader.
|
||||
* Allows to specify instance of cache and image loader used with image manager.
|
||||
* @return new instance of `SDWebImageManager` with specified cache and loader.
|
||||
*/
|
||||
- (nonnull instancetype)initWithCache:(nonnull id<SDWebImageCache>)cache downloader:(nonnull id<SDWebImageLoader>)downloader NS_DESIGNATED_INITIALIZER;
|
||||
- (nonnull instancetype)initWithCache:(nonnull id<SDWebImageCache>)cache loader:(nonnull id<SDWebImageLoader>)loader NS_DESIGNATED_INITIALIZER;
|
||||
|
||||
/**
|
||||
* Downloads the image at the given URL if not present in cache or return the cached version otherwise.
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
#import "SDWebImageError.h"
|
||||
|
||||
static id<SDWebImageCache> _defaultImageCache;
|
||||
static SDWebImageDownloader *_defaultImageDownloader;
|
||||
static id<SDWebImageLoader> _defaultImageLoader;
|
||||
|
||||
@interface SDWebImageCombinedOperation ()
|
||||
|
||||
|
@ -28,7 +28,7 @@ static SDWebImageDownloader *_defaultImageDownloader;
|
|||
@interface SDWebImageManager ()
|
||||
|
||||
@property (strong, nonatomic, readwrite, nonnull) SDImageCache *imageCache;
|
||||
@property (strong, nonatomic, readwrite, nonnull) id<SDWebImageLoader> imageDownloader;
|
||||
@property (strong, nonatomic, readwrite, nonnull) id<SDWebImageLoader> imageLoader;
|
||||
@property (strong, nonatomic, nonnull) NSMutableSet<NSURL *> *failedURLs;
|
||||
@property (strong, nonatomic, nonnull) NSMutableArray<SDWebImageCombinedOperation *> *runningOperations;
|
||||
|
||||
|
@ -47,15 +47,15 @@ static SDWebImageDownloader *_defaultImageDownloader;
|
|||
_defaultImageCache = defaultImageCache;
|
||||
}
|
||||
|
||||
+ (SDWebImageDownloader *)defaultImageDownloader {
|
||||
return _defaultImageDownloader;
|
||||
+ (id<SDWebImageLoader>)defaultImageLoader {
|
||||
return _defaultImageLoader;
|
||||
}
|
||||
|
||||
+ (void)setDefaultImageDownloader:(SDWebImageDownloader *)defaultImageDownloader {
|
||||
if (defaultImageDownloader && ![defaultImageDownloader isKindOfClass:[SDWebImageDownloader class]]) {
|
||||
+ (void)setDefaultImageLoader:(id<SDWebImageLoader>)defaultImageLoader {
|
||||
if (defaultImageLoader && ![defaultImageLoader conformsToProtocol:@protocol(SDWebImageLoader)]) {
|
||||
return;
|
||||
}
|
||||
_defaultImageDownloader = defaultImageDownloader;
|
||||
_defaultImageLoader = defaultImageLoader;
|
||||
}
|
||||
|
||||
+ (nonnull instancetype)sharedManager {
|
||||
|
@ -72,17 +72,17 @@ static SDWebImageDownloader *_defaultImageDownloader;
|
|||
if (!cache) {
|
||||
cache = [SDImageCache sharedImageCache];
|
||||
}
|
||||
SDWebImageDownloader *downloader = [[self class] defaultImageDownloader];
|
||||
if (!downloader) {
|
||||
downloader = [SDWebImageDownloader sharedDownloader];
|
||||
id<SDWebImageLoader> loader = [[self class] defaultImageLoader];
|
||||
if (!loader) {
|
||||
loader = [SDWebImageDownloader sharedDownloader];
|
||||
}
|
||||
return [self initWithCache:cache downloader:downloader];
|
||||
return [self initWithCache:cache loader:loader];
|
||||
}
|
||||
|
||||
- (nonnull instancetype)initWithCache:(nonnull id<SDWebImageCache>)cache downloader:(nonnull id<SDWebImageLoader>)downloader {
|
||||
- (nonnull instancetype)initWithCache:(nonnull id<SDWebImageCache>)cache loader:(nonnull id<SDWebImageLoader>)loader {
|
||||
if ((self = [super init])) {
|
||||
_imageCache = cache;
|
||||
_imageDownloader = downloader;
|
||||
_imageLoader = loader;
|
||||
_failedURLs = [NSMutableSet new];
|
||||
_runningOperations = [NSMutableArray new];
|
||||
}
|
||||
|
@ -195,8 +195,21 @@ static SDWebImageDownloader *_defaultImageDownloader;
|
|||
BOOL shouldDownload = (!(options & SDWebImageFromCacheOnly))
|
||||
&& (!cachedImage || options & SDWebImageRefreshCached)
|
||||
&& (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url]);
|
||||
// Check whether image downloader support target URL
|
||||
shouldDownload &= [self.imageLoader canLoadWithURL:url];
|
||||
if (shouldDownload) {
|
||||
SDWebImageContext *downloadContext = context;
|
||||
if (cacheKeyFilter) {
|
||||
// Pass the cache key filter to the image loader.
|
||||
SDWebImageMutableContext *mutableContext;
|
||||
if (downloadContext) {
|
||||
mutableContext = [downloadContext mutableCopy];
|
||||
} else {
|
||||
mutableContext = [NSMutableDictionary dictionary];
|
||||
}
|
||||
[mutableContext setValue:cacheKeyFilter forKey:SDWebImageContextCacheKeyFilter];
|
||||
downloadContext = [mutableContext copy];
|
||||
}
|
||||
if (cachedImage && options & SDWebImageRefreshCached) {
|
||||
// If image was found in the cache but SDWebImageRefreshCached is provided, notify about the cached image
|
||||
// AND try to re-download it in order to let a chance to NSURLCache to refresh it from server.
|
||||
|
@ -214,7 +227,7 @@ static SDWebImageDownloader *_defaultImageDownloader;
|
|||
|
||||
// `SDWebImageCombinedOperation` -> `SDWebImageDownloadToken` -> `downloadOperationCancelToken`, which is a `SDCallbacksDictionary` and retain the completed block below, so we need weak-strong again to avoid retain cycle
|
||||
__weak typeof(strongOperation) weakSubOperation = strongOperation;
|
||||
strongOperation.downloadOperation = [self.imageDownloader loadImageWithURL:url options:options context:downloadContext progress:progressBlock completed:^(UIImage *downloadedImage, NSData *downloadedData, NSError *error, BOOL finished) {
|
||||
strongOperation.downloadOperation = [self.imageLoader loadImageWithURL:url options:options context:downloadContext progress:progressBlock completed:^(UIImage *downloadedImage, NSData *downloadedData, NSError *error, BOOL finished) {
|
||||
__strong typeof(weakSubOperation) strongSubOperation = weakSubOperation;
|
||||
if (!strongSubOperation || strongSubOperation.isCancelled) {
|
||||
// Do nothing if the operation was cancelled
|
||||
|
|
|
@ -118,7 +118,7 @@
|
|||
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
|
||||
NSString *testImagePath = [testBundle pathForResource:@"TestImage" ofType:@"jpg"];
|
||||
transformer.testImage = [[UIImage alloc] initWithContentsOfFile:testImagePath];
|
||||
SDWebImageManager *manager = [[SDWebImageManager alloc] initWithCache:[SDImageCache sharedImageCache] downloader:[SDWebImageDownloader sharedDownloader]];
|
||||
SDWebImageManager *manager = [[SDWebImageManager alloc] initWithCache:[SDImageCache sharedImageCache] loader:[SDWebImageDownloader sharedDownloader]];
|
||||
manager.transformer = transformer;
|
||||
[[SDImageCache sharedImageCache] removeImageForKey:kTestJpegURL withCompletion:^{
|
||||
[manager loadImageWithURL:imageURL options:SDWebImageTransformAnimatedImage progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
|
||||
|
|
Loading…
Reference in New Issue