Add transformer property in manager level to allow a central control of image transform(optional)

This commit is contained in:
DreamPiggy 2018-02-18 01:22:07 +08:00
parent 464d725368
commit 44d266af7c
4 changed files with 24 additions and 8 deletions

View File

@ -22,6 +22,6 @@ FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextSetIma
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextCustomManager;
/**
A id<SDWebImageTransformer> instance which conforms SDWebImageTransformer protocol. It's used for image transform after the image load finished and store the transformed image to cache. (id<SDWebImageTransformer>)
A id<SDWebImageTransformer> instance which conforms SDWebImageTransformer protocol. It's used for image transform after the image load finished and store the transformed image to cache. If you provide one, it will ignore the `transformer` in manager and use provided one instead. (id<SDWebImageTransformer>)
*/
FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextCustomTransformer;

View File

@ -10,6 +10,7 @@
#import "SDWebImageOperation.h"
#import "SDWebImageDownloader.h"
#import "SDImageCache.h"
#import "SDWebImageTransformer.h"
typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
/**
@ -183,6 +184,13 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager];
@property (strong, nonatomic, readonly, nullable) SDImageCache *imageCache;
@property (strong, nonatomic, readonly, nullable) SDWebImageDownloader *imageDownloader;
/**
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`.
Defaults to nil, which means no transform is applied.
@note This will affect all the load requests for this manager if you provide. However, you can pass `SDWebImageContextCustomTransformer` in context arg to explicitly use that transformer instead.
*/
@property (strong, nonatomic, nullable) id<SDWebImageTransformer> transformer;
/**
* The cache filter is a block used each time SDWebImageManager need to convert an URL into a cache key. This can
* be used to remove dynamic part of an image URL.

View File

@ -8,8 +8,6 @@
#import "SDWebImageManager.h"
#import "NSImage+Additions.h"
#import <objc/message.h>
#import "SDWebImageTransformer.h"
@interface SDWebImageCombinedOperation : NSObject <SDWebImageOperation>
@ -156,6 +154,18 @@
if (options & SDWebImageQueryDiskSync) cacheOptions |= SDImageCacheQueryDiskSync;
if (options & SDWebImageTransformAnimatedImage) cacheOptions |= SDImageCacheTransformAnimatedImage;
// Image transformer
id<SDWebImageTransformer> transformer;
if ([context valueForKey:SDWebImageContextCustomTransformer]) {
transformer = [context valueForKey:SDWebImageContextCustomTransformer];
} else if (self.transformer) {
// Transformer from manager
transformer = self.transformer;
NSMutableDictionary<SDWebImageContextOption, id> *mutableContext = [NSMutableDictionary dictionaryWithDictionary:context];
[mutableContext setValue:transformer forKey:SDWebImageContextCustomTransformer];
context = [mutableContext copy];
}
__weak SDWebImageCombinedOperation *weakOperation = operation;
operation.cacheOperation = [self.imageCache queryCacheOperationForKey:key options:cacheOptions done:^(UIImage *cachedImage, NSData *cachedData, SDImageCacheType cacheType) {
__strong __typeof(weakOperation) strongOperation = weakOperation;
@ -240,9 +250,8 @@
if (options & SDWebImageRefreshCached && cachedImage && !downloadedImage) {
// Image refresh hit the NSURLCache cache, do not call the completion block
} else if (downloadedImage && (!downloadedImage.images || (options & SDWebImageTransformAnimatedImage)) && [context valueForKey:SDWebImageContextCustomTransformer]) {
} else if (downloadedImage && (!downloadedImage.images || (options & SDWebImageTransformAnimatedImage)) && transformer) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
id<SDWebImageTransformer> transformer = [context valueForKey:SDWebImageContextCustomTransformer];
UIImage *transformedImage = [transformer transformedImageWithImage:downloadedImage forKey:key];
if (transformedImage && finished) {
NSString *transformerKey = [transformer transformerKey];

View File

@ -11,8 +11,7 @@
/**
A transformer protocol to transform the image load from cache or from download.
For cache, it store the transformed image to memory cache if the memory cache missed (Default).
For download, it store the transformed image to disk cache and memory cache if the download finished (Default).
You can provide transformer to cache and manager (Through the `transformer` property or context option `SDWebImageContextCustomTransformer`).
@note The transform process is called from a global queue in order to not to block the main queue.
*/
@ -42,7 +41,7 @@
// Separator for different transformerKey, for example, `image.png` |> flip(YES,NO) |> rotate(pi/4,YES) => 'image-SDWebImageFlippingTransformer(1,0)-SDWebImageRotationTransformer(0.78539816339,1).png'
FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageTransformerKeySeparator;
// Pipeline transformer. Which you can bind multiple transformers together to let the image to be transformed one by one and generate the final image.
// Pipeline transformer. Which you can bind multiple transformers together to let the image to be transformed one by one in order and generate the final image.
@interface SDWebImagePipelineTransformer : NSObject <SDWebImageTransformer>
@property (nonatomic, copy, readonly, nonnull) NSArray<id<SDWebImageTransformer>> *transformers;