SDImageAssetManager.m 4.6 KB

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