NSBundle+YYAdd.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // NSBundle+YYAdd.m
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 14/10/20.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import "NSBundle+YYAdd.h"
  12. #import "NSString+YYAdd.h"
  13. #import "YYKitMacro.h"
  14. YYSYNTH_DUMMY_CLASS(NSBundle_YYAdd)
  15. @implementation NSBundle (YYAdd)
  16. + (NSArray *)preferredScales {
  17. static NSArray *scales;
  18. static dispatch_once_t onceToken;
  19. dispatch_once(&onceToken, ^{
  20. CGFloat screenScale = [UIScreen mainScreen].scale;
  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. + (NSString *)pathForScaledResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)bundlePath {
  32. if (name.length == 0) return nil;
  33. if ([name hasSuffix:@"/"]) return [self pathForResource:name ofType:ext inDirectory:bundlePath];
  34. NSString *path = nil;
  35. NSArray *scales = [self preferredScales];
  36. for (int s = 0; s < scales.count; s++) {
  37. CGFloat scale = ((NSNumber *)scales[s]).floatValue;
  38. NSString *scaledName = ext.length ? [name stringByAppendingNameScale:scale]
  39. : [name stringByAppendingPathScale:scale];
  40. path = [self pathForResource:scaledName ofType:ext inDirectory:bundlePath];
  41. if (path) break;
  42. }
  43. return path;
  44. }
  45. - (NSString *)pathForScaledResource:(NSString *)name ofType:(NSString *)ext {
  46. if (name.length == 0) return nil;
  47. if ([name hasSuffix:@"/"]) return [self pathForResource:name ofType:ext];
  48. NSString *path = nil;
  49. NSArray *scales = [NSBundle preferredScales];
  50. for (int s = 0; s < scales.count; s++) {
  51. CGFloat scale = ((NSNumber *)scales[s]).floatValue;
  52. NSString *scaledName = ext.length ? [name stringByAppendingNameScale:scale]
  53. : [name stringByAppendingPathScale:scale];
  54. path = [self pathForResource:scaledName ofType:ext];
  55. if (path) break;
  56. }
  57. return path;
  58. }
  59. - (NSString *)pathForScaledResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)subpath {
  60. if (name.length == 0) return nil;
  61. if ([name hasSuffix:@"/"]) return [self pathForResource:name ofType:ext];
  62. NSString *path = nil;
  63. NSArray *scales = [NSBundle preferredScales];
  64. for (int s = 0; s < scales.count; s++) {
  65. CGFloat scale = ((NSNumber *)scales[s]).floatValue;
  66. NSString *scaledName = ext.length ? [name stringByAppendingNameScale:scale]
  67. : [name stringByAppendingPathScale:scale];
  68. path = [self pathForResource:scaledName ofType:ext inDirectory:subpath];
  69. if (path) break;
  70. }
  71. return path;
  72. }
  73. @end