SDWebImageDefine.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "SDWebImageDefine.h"
  9. #import "UIImage+Metadata.h"
  10. #import "NSImage+Compatibility.h"
  11. #pragma mark - Image scale
  12. static inline NSArray<NSNumber *> * _Nonnull SDImageScaleFactors() {
  13. return @[@2, @3];
  14. }
  15. inline CGFloat SDImageScaleFactorForKey(NSString * _Nullable key) {
  16. CGFloat scale = 1;
  17. if (!key) {
  18. return scale;
  19. }
  20. // Check if target OS support scale
  21. #if SD_WATCH
  22. if ([[WKInterfaceDevice currentDevice] respondsToSelector:@selector(screenScale)])
  23. #elif SD_UIKIT
  24. if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
  25. #elif SD_MAC
  26. if ([[NSScreen mainScreen] respondsToSelector:@selector(backingScaleFactor)])
  27. #endif
  28. {
  29. // a@2x.png -> 8
  30. if (key.length >= 8) {
  31. // Fast check
  32. BOOL isURL = [key hasPrefix:@"http://"] || [key hasPrefix:@"https://"];
  33. for (NSNumber *scaleFactor in SDImageScaleFactors()) {
  34. // @2x. for file name and normal url
  35. NSString *fileScale = [NSString stringWithFormat:@"@%@x.", scaleFactor];
  36. if ([key containsString:fileScale]) {
  37. scale = scaleFactor.doubleValue;
  38. return scale;
  39. }
  40. if (isURL) {
  41. // %402x. for url encode
  42. NSString *urlScale = [NSString stringWithFormat:@"%%40%@x.", scaleFactor];
  43. if ([key containsString:urlScale]) {
  44. scale = scaleFactor.doubleValue;
  45. return scale;
  46. }
  47. }
  48. }
  49. }
  50. }
  51. return scale;
  52. }
  53. inline UIImage * _Nullable SDScaledImageForKey(NSString * _Nullable key, UIImage * _Nullable image) {
  54. if (!image) {
  55. return nil;
  56. }
  57. CGFloat scale = SDImageScaleFactorForKey(key);
  58. return SDScaledImageForScaleFactor(scale, image);
  59. }
  60. inline UIImage * _Nullable SDScaledImageForScaleFactor(CGFloat scale, UIImage * _Nullable image) {
  61. if (!image) {
  62. return nil;
  63. }
  64. if (scale <= 1) {
  65. return image;
  66. }
  67. if (scale == image.scale) {
  68. return image;
  69. }
  70. UIImage *scaledImage;
  71. if (image.sd_isAnimated) {
  72. UIImage *animatedImage;
  73. #if SD_UIKIT || SD_WATCH
  74. // `UIAnimatedImage` images share the same size and scale.
  75. NSMutableArray<UIImage *> *scaledImages = [NSMutableArray array];
  76. for (UIImage *tempImage in image.images) {
  77. UIImage *tempScaledImage = [[UIImage alloc] initWithCGImage:tempImage.CGImage scale:scale orientation:tempImage.imageOrientation];
  78. [scaledImages addObject:tempScaledImage];
  79. }
  80. animatedImage = [UIImage animatedImageWithImages:scaledImages duration:image.duration];
  81. animatedImage.sd_imageLoopCount = image.sd_imageLoopCount;
  82. #else
  83. // Animated GIF for `NSImage` need to grab `NSBitmapImageRep`;
  84. NSRect imageRect = NSMakeRect(0, 0, image.size.width, image.size.height);
  85. NSImageRep *imageRep = [image bestRepresentationForRect:imageRect context:nil hints:nil];
  86. NSBitmapImageRep *bitmapImageRep;
  87. if ([imageRep isKindOfClass:[NSBitmapImageRep class]]) {
  88. bitmapImageRep = (NSBitmapImageRep *)imageRep;
  89. }
  90. if (bitmapImageRep) {
  91. NSSize size = NSMakeSize(image.size.width / scale, image.size.height / scale);
  92. animatedImage = [[NSImage alloc] initWithSize:size];
  93. bitmapImageRep.size = size;
  94. [animatedImage addRepresentation:bitmapImageRep];
  95. }
  96. #endif
  97. scaledImage = animatedImage;
  98. } else {
  99. #if SD_UIKIT || SD_WATCH
  100. scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation];
  101. #else
  102. scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:kCGImagePropertyOrientationUp];
  103. #endif
  104. }
  105. scaledImage.sd_isIncremental = image.sd_isIncremental;
  106. scaledImage.sd_imageFormat = image.sd_imageFormat;
  107. return scaledImage;
  108. }
  109. #pragma mark - Context option
  110. SDWebImageContextOption const SDWebImageContextSetImageOperationKey = @"setImageOperationKey";
  111. SDWebImageContextOption const SDWebImageContextCustomManager = @"customManager";
  112. SDWebImageContextOption const SDWebImageContextImageTransformer = @"imageTransformer";
  113. SDWebImageContextOption const SDWebImageContextImageScaleFactor = @"imageScaleFactor";
  114. SDWebImageContextOption const SDWebImageContextStoreCacheType = @"storeCacheType";
  115. SDWebImageContextOption const SDWebImageContextAnimatedImageClass = @"animatedImageClass";
  116. SDWebImageContextOption const SDWebImageContextDownloadRequestModifier = @"downloadRequestModifier";
  117. SDWebImageContextOption const SDWebImageContextCacheKeyFilter = @"cacheKeyFilter";
  118. SDWebImageContextOption const SDWebImageContextCacheSerializer = @"cacheSerializer";