Repository: SDWebImage/SDWebImage
File: SDWebImage/Core/SDImageCacheDefine.m (Objective-C)

Code before:
/*
 * This file is part of the SDWebImage package.
 * (c) Olivier Poitrey <rs@dailymotion.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

#import "SDImageCacheDefine.h"
#import "SDImageCodersManager.h"
#import "SDImageCoderHelper.h"
#import "SDAnimatedImage.h"
#import "UIImage+Metadata.h"
#import "SDInternalMacros.h"

SDImageCoderOptions * _Nonnull SDGetDecodeOptionsFromContext(SDWebImageContext * _Nullable context, SDWebImageOptions options, NSString * _Nonnull cacheKey) {
    BOOL decodeFirstFrame = SD_OPTIONS_CONTAINS(options, SDWebImageDecodeFirstFrameOnly);
    NSNumber *scaleValue = context[SDWebImageContextImageScaleFactor];
    CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey);
    NSNumber *preserveAspectRatioValue = context[SDWebImageContextImagePreserveAspectRatio];
    NSValue *thumbnailSizeValue;
    BOOL shouldScaleDown = SD_OPTIONS_CONTAINS(options, SDWebImageScaleDownLargeImages);
    if (shouldScaleDown) {
        CGFloat thumbnailPixels = SDImageCoderHelper.defaultScaleDownLimitBytes / 4;
        CGFloat dimension = ceil(sqrt(thumbnailPixels));
        thumbnailSizeValue = @(CGSizeMake(dimension, dimension));
    }
    if (context[SDWebImageContextImageThumbnailPixelSize]) {
        thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize];
    }
    
    SDImageCoderMutableOptions *mutableCoderOptions = [NSMutableDictionary dictionaryWithCapacity:2];
    mutableCoderOptions[SDImageCoderDecodeFirstFrameOnly] = @(decodeFirstFrame);
    mutableCoderOptions[SDImageCoderDecodeScaleFactor] = @(scale);
    mutableCoderOptions[SDImageCoderDecodePreserveAspectRatio] = preserveAspectRatioValue;
    mutableCoderOptions[SDImageCoderDecodeThumbnailPixelSize] = thumbnailSizeValue;
    mutableCoderOptions[SDImageCoderWebImageContext] = context;
    SDImageCoderOptions *coderOptions = [mutableCoderOptions copy];
    
    return coderOptions;
}

Review comment (refactor):
Hack for 5.13. In 5.14 I will remove this and use new `SDWebImageContextImageDecodeOptions` instead.

See more in PR #3423 

Code after:
/*
 * This file is part of the SDWebImage package.
 * (c) Olivier Poitrey <rs@dailymotion.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

#import "SDImageCacheDefine.h"
#import "SDImageCodersManager.h"
#import "SDImageCoderHelper.h"
#import "SDAnimatedImage.h"
#import "UIImage+Metadata.h"
#import "SDInternalMacros.h"

static NSArray<NSString *>* GetKnownContextOptions(void) {
    static NSArray<NSString *> *knownContextOptions;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        knownContextOptions =
        [NSArray arrayWithObjects:
         SDWebImageContextSetImageOperationKey,
         SDWebImageContextCustomManager,
         SDWebImageContextImageCache,
         SDWebImageContextImageLoader,
         SDWebImageContextImageCoder,
         SDWebImageContextImageTransformer,
         SDWebImageContextImageScaleFactor,
         SDWebImageContextImagePreserveAspectRatio,
         SDWebImageContextImageThumbnailPixelSize,
         SDWebImageContextQueryCacheType,
         SDWebImageContextStoreCacheType,
         SDWebImageContextOriginalQueryCacheType,
         SDWebImageContextOriginalStoreCacheType,
         SDWebImageContextOriginalImageCache,
         SDWebImageContextAnimatedImageClass,
         SDWebImageContextDownloadRequestModifier,
         SDWebImageContextDownloadResponseModifier,
         SDWebImageContextDownloadDecryptor,
         SDWebImageContextCacheKeyFilter,
         SDWebImageContextCacheSerializer