SDImageAssetManager.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "SDImageAssetManager.h"
  9. #import "SDInternalMacros.h"
  10. static NSArray *SDBundlePreferredScales(void) {
  11. static NSArray *scales;
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. #if SD_WATCH
  15. CGFloat screenScale = [WKInterfaceDevice currentDevice].screenScale;
  16. #elif SD_UIKIT
  17. CGFloat screenScale = [UIScreen mainScreen].scale;
  18. #elif SD_MAC
  19. NSScreen *mainScreen = nil;
  20. if (@available(macOS 10.12, *)) {
  21. mainScreen = [NSScreen mainScreen];
  22. } else {
  23. mainScreen = [NSScreen screens].firstObject;
  24. }
  25. CGFloat screenScale = mainScreen.backingScaleFactor ?: 1.0f;
  26. #endif
  27. if (screenScale <= 1) {
  28. scales = @[@1,@2,@3];
  29. } else if (screenScale <= 2) {
  30. scales = @[@2,@3,@1];
  31. } else {
  32. scales = @[@3,@2,@1];
  33. }
  34. });
  35. return scales;
  36. }
  37. @implementation SDImageAssetManager {
  38. SD_LOCK_DECLARE(_lock);
  39. }
  40. + (instancetype)sharedAssetManager {
  41. static dispatch_once_t onceToken;
  42. static SDImageAssetManager *assetManager;
  43. dispatch_once(&onceToken, ^{
  44. assetManager = [[SDImageAssetManager alloc] init];
  45. });
  46. return assetManager;
  47. }
  48. - (instancetype)init {
  49. self = [super init];
  50. if (self) {
  51. NSPointerFunctionsOptions valueOptions;
  52. #if SD_MAC
  53. // Apple says that NSImage use a weak reference to value
  54. valueOptions = NSPointerFunctionsWeakMemory;
  55. #else
  56. // Apple says that UIImage use a strong reference to value
  57. valueOptions = NSPointerFunctionsStrongMemory;
  58. #endif
  59. _imageTable = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsCopyIn valueOptions:valueOptions];
  60. SD_LOCK_INIT(_lock);
  61. #if SD_UIKIT
  62. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
  63. #endif
  64. }
  65. return self;
  66. }
  67. - (void)dealloc {
  68. #if SD_UIKIT
  69. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
  70. #endif
  71. }
  72. - (void)didReceiveMemoryWarning:(NSNotification *)notification {
  73. SD_LOCK(_lock);
  74. [self.imageTable removeAllObjects];
  75. SD_UNLOCK(_lock);
  76. }
  77. - (NSString *)getPathForName:(NSString *)name bundle:(NSBundle *)bundle preferredScale:(CGFloat *)scale {
  78. NSParameterAssert(name);
  79. NSParameterAssert(bundle);
  80. NSString *path;
  81. if (name.length == 0) {
  82. return path;
  83. }
  84. if ([name hasSuffix:@"/"]) {
  85. return path;
  86. }
  87. NSString *extension = name.pathExtension;
  88. if (extension.length == 0) {
  89. // If no extension, follow Apple's doc, check PNG format
  90. extension = @"png";
  91. }
  92. name = [name stringByDeletingPathExtension];
  93. CGFloat providedScale = *scale;
  94. NSArray *scales = SDBundlePreferredScales();
  95. // Check if file name contains scale
  96. for (size_t i = 0; i < scales.count; i++) {
  97. NSNumber *scaleValue = scales[i];
  98. if ([name hasSuffix:[NSString stringWithFormat:@"@%@x", scaleValue]]) {
  99. path = [bundle pathForResource:name ofType:extension];
  100. if (path) {
  101. *scale = scaleValue.doubleValue; // override
  102. return path;
  103. }
  104. }
  105. }
  106. // Search with provided scale first
  107. if (providedScale != 0) {
  108. NSString *scaledName = [name stringByAppendingFormat:@"@%@x", @(providedScale)];
  109. path = [bundle pathForResource:scaledName ofType:extension];
  110. if (path) {
  111. return path;
  112. }
  113. }
  114. // Search with preferred scale
  115. for (size_t i = 0; i < scales.count; i++) {
  116. NSNumber *scaleValue = scales[i];
  117. if (scaleValue.doubleValue == providedScale) {
  118. // Ignore provided scale
  119. continue;
  120. }
  121. NSString *scaledName = [name stringByAppendingFormat:@"@%@x", scaleValue];
  122. path = [bundle pathForResource:scaledName ofType:extension];
  123. if (path) {
  124. *scale = scaleValue.doubleValue; // override
  125. return path;
  126. }
  127. }
  128. // Search without scale
  129. path = [bundle pathForResource:name ofType:extension];
  130. return path;
  131. }
  132. - (UIImage *)imageForName:(NSString *)name {
  133. NSParameterAssert(name);
  134. UIImage *image;
  135. SD_LOCK(_lock);
  136. image = [self.imageTable objectForKey:name];
  137. SD_UNLOCK(_lock);
  138. return image;
  139. }
  140. - (void)storeImage:(UIImage *)image forName:(NSString *)name {
  141. NSParameterAssert(image);
  142. NSParameterAssert(name);
  143. SD_LOCK(_lock);
  144. [self.imageTable setObject:image forKey:name];
  145. SD_UNLOCK(_lock);
  146. }
  147. @end