NSBundle+YYAdd.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // NSBundle+YYAdd.m
  3. // YYCategories <https://github.com/ibireme/YYCategories>
  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. @implementation NSBundle (YYAdd)
  14. + (NSArray *)preferredScales {
  15. static NSArray *scales;
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. CGFloat screenScale = [UIScreen mainScreen].scale;
  19. if (screenScale <= 1) {
  20. scales = @[@1,@2,@3];
  21. } else if (screenScale <= 2) {
  22. scales = @[@2,@3,@1];
  23. } else {
  24. scales = @[@3,@2,@1];
  25. }
  26. });
  27. return scales;
  28. }
  29. + (NSString *)pathForScaledResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)bundlePath {
  30. if (name.length == 0) return nil;
  31. if ([name hasSuffix:@"/"]) return [self pathForResource:name ofType:ext inDirectory:bundlePath];
  32. NSString *path = nil;
  33. NSArray *scales = [self preferredScales];
  34. for (int s = 0; s < scales.count; s++) {
  35. CGFloat scale = ((NSNumber *)scales[s]).floatValue;
  36. NSString *scaledName = ext.length ? [name stringByAppendingNameScale:scale]
  37. : [name stringByAppendingPathScale:scale];
  38. path = [self pathForResource:scaledName ofType:ext inDirectory:bundlePath];
  39. if (path) break;
  40. }
  41. return path;
  42. }
  43. - (NSString *)pathForScaledResource:(NSString *)name ofType:(NSString *)ext {
  44. if (name.length == 0) return nil;
  45. if ([name hasSuffix:@"/"]) return [self pathForResource:name ofType:ext];
  46. NSString *path = nil;
  47. NSArray *scales = [NSBundle preferredScales];
  48. for (int s = 0; s < scales.count; s++) {
  49. CGFloat scale = ((NSNumber *)scales[s]).floatValue;
  50. NSString *scaledName = ext.length ? [name stringByAppendingNameScale:scale]
  51. : [name stringByAppendingPathScale:scale];
  52. path = [self pathForResource:scaledName ofType:ext];
  53. if (path) break;
  54. }
  55. return path;
  56. }
  57. - (NSString *)pathForScaledResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)subpath {
  58. if (name.length == 0) return nil;
  59. if ([name hasSuffix:@"/"]) return [self pathForResource:name ofType:ext];
  60. NSString *path = nil;
  61. NSArray *scales = [NSBundle preferredScales];
  62. for (int s = 0; s < scales.count; s++) {
  63. CGFloat scale = ((NSNumber *)scales[s]).floatValue;
  64. NSString *scaledName = ext.length ? [name stringByAppendingNameScale:scale]
  65. : [name stringByAppendingPathScale:scale];
  66. path = [self pathForResource:scaledName ofType:ext inDirectory:subpath];
  67. if (path) break;
  68. }
  69. return path;
  70. }
  71. @end