From 44d266af7c26d191be337f183439d129b08047df Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sun, 18 Feb 2018 01:22:07 +0800 Subject: [PATCH] Add transformer property in manager level to allow a central control of image transform(optional) --- SDWebImage/SDWebImageDefine.h | 2 +- SDWebImage/SDWebImageManager.h | 8 ++++++++ SDWebImage/SDWebImageManager.m | 17 +++++++++++++---- SDWebImage/SDWebImageTransformer.h | 5 ++--- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/SDWebImage/SDWebImageDefine.h b/SDWebImage/SDWebImageDefine.h index b3f5e726..4437d578 100644 --- a/SDWebImage/SDWebImageDefine.h +++ b/SDWebImage/SDWebImageDefine.h @@ -22,6 +22,6 @@ FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextSetIma FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextCustomManager; /** - A id instance which conforms SDWebImageTransformer protocol. It's used for image transform after the image load finished and store the transformed image to cache. (id) + A id 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) */ FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextCustomTransformer; diff --git a/SDWebImage/SDWebImageManager.h b/SDWebImage/SDWebImageManager.h index baf66ddb..d28ad8f7 100644 --- a/SDWebImage/SDWebImageManager.h +++ b/SDWebImage/SDWebImageManager.h @@ -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 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. diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 5e9ae917..c30a612d 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -8,8 +8,6 @@ #import "SDWebImageManager.h" #import "NSImage+Additions.h" -#import -#import "SDWebImageTransformer.h" @interface SDWebImageCombinedOperation : NSObject @@ -156,6 +154,18 @@ if (options & SDWebImageQueryDiskSync) cacheOptions |= SDImageCacheQueryDiskSync; if (options & SDWebImageTransformAnimatedImage) cacheOptions |= SDImageCacheTransformAnimatedImage; + // Image transformer + id transformer; + if ([context valueForKey:SDWebImageContextCustomTransformer]) { + transformer = [context valueForKey:SDWebImageContextCustomTransformer]; + } else if (self.transformer) { + // Transformer from manager + transformer = self.transformer; + NSMutableDictionary *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 transformer = [context valueForKey:SDWebImageContextCustomTransformer]; UIImage *transformedImage = [transformer transformedImageWithImage:downloadedImage forKey:key]; if (transformedImage && finished) { NSString *transformerKey = [transformer transformerKey]; diff --git a/SDWebImage/SDWebImageTransformer.h b/SDWebImage/SDWebImageTransformer.h index 8080e47f..a060e6a3 100644 --- a/SDWebImage/SDWebImageTransformer.h +++ b/SDWebImage/SDWebImageTransformer.h @@ -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 @property (nonatomic, copy, readonly, nonnull) NSArray> *transformers;