SDImageCacheDefine.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "SDImageCacheDefine.h"
  9. #import "SDImageCodersManager.h"
  10. #import "SDImageCoderHelper.h"
  11. #import "SDAnimatedImage.h"
  12. #import "UIImage+Metadata.h"
  13. UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSString * _Nonnull cacheKey, SDWebImageOptions options, SDWebImageContext * _Nullable context) {
  14. UIImage *image;
  15. BOOL decodeFirstFrame = options & SDWebImageDecodeFirstFrameOnly;
  16. NSNumber *scaleValue = context[SDWebImageContextImageScaleFactor];
  17. CGFloat scale = scaleValue.doubleValue >= 1 ? scaleValue.doubleValue : SDImageScaleFactorForKey(cacheKey);
  18. SDImageCoderOptions *coderOptions = @{SDImageCoderDecodeFirstFrameOnly : @(decodeFirstFrame), SDImageCoderDecodeScaleFactor : @(scale)};
  19. if (context) {
  20. SDImageCoderMutableOptions *mutableCoderOptions = [coderOptions mutableCopy];
  21. [mutableCoderOptions setValue:context forKey:SDImageCoderWebImageContext];
  22. coderOptions = [mutableCoderOptions copy];
  23. }
  24. if (!decodeFirstFrame) {
  25. Class animatedImageClass = context[SDWebImageContextAnimatedImageClass];
  26. // check whether we should use `SDAnimatedImage`
  27. if ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)]) {
  28. image = [[animatedImageClass alloc] initWithData:imageData scale:scale options:coderOptions];
  29. if (options & SDWebImagePreloadAllFrames && [image respondsToSelector:@selector(preloadAllFrames)]) {
  30. [((id<SDAnimatedImage>)image) preloadAllFrames];
  31. }
  32. }
  33. }
  34. if (!image) {
  35. image = [[SDImageCodersManager sharedManager] decodedImageWithData:imageData options:coderOptions];
  36. }
  37. if (image) {
  38. BOOL shouldDecode = (options & SDWebImageAvoidDecodeImage) == 0;
  39. if ([image conformsToProtocol:@protocol(SDAnimatedImage)]) {
  40. // `SDAnimatedImage` do not decode
  41. shouldDecode = NO;
  42. } else if (image.sd_isAnimated) {
  43. // animated image do not decode
  44. shouldDecode = NO;
  45. }
  46. if (shouldDecode) {
  47. BOOL shouldScaleDown = options & SDWebImageScaleDownLargeImages;
  48. if (shouldScaleDown) {
  49. image = [SDImageCoderHelper decodedAndScaledDownImageWithImage:image limitBytes:0];
  50. } else {
  51. image = [SDImageCoderHelper decodedImageWithImage:image];
  52. }
  53. }
  54. }
  55. return image;
  56. }