You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

87 lines
2.8 KiB

//
// NSBundle+YYAdd.m
// YYKit <https://github.com/ibireme/YYKit>
//
// Created by ibireme on 14/10/20.
// Copyright (c) 2015 ibireme.
//
// This source code is licensed under the MIT-style license found in the
// LICENSE file in the root directory of this source tree.
//
#import "NSBundle+YYAdd.h"
#import "NSString+YYAdd.h"
#import "YYKitMacro.h"
YYSYNTH_DUMMY_CLASS(NSBundle_YYAdd)
@implementation NSBundle (YYAdd)
+ (NSArray *)preferredScales {
static NSArray *scales;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
CGFloat screenScale = [UIScreen mainScreen].scale;
if (screenScale <= 1) {
scales = @[@1,@2,@3];
} else if (screenScale <= 2) {
scales = @[@2,@3,@1];
} else {
scales = @[@3,@2,@1];
}
});
return scales;
}
+ (NSString *)pathForScaledResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)bundlePath {
if (name.length == 0) return nil;
if ([name hasSuffix:@"/"]) return [self pathForResource:name ofType:ext inDirectory:bundlePath];
NSString *path = nil;
NSArray *scales = [self preferredScales];
for (int s = 0; s < scales.count; s++) {
CGFloat scale = ((NSNumber *)scales[s]).floatValue;
NSString *scaledName = ext.length ? [name stringByAppendingNameScale:scale]
: [name stringByAppendingPathScale:scale];
path = [self pathForResource:scaledName ofType:ext inDirectory:bundlePath];
if (path) break;
}
return path;
}
- (NSString *)pathForScaledResource:(NSString *)name ofType:(NSString *)ext {
if (name.length == 0) return nil;
if ([name hasSuffix:@"/"]) return [self pathForResource:name ofType:ext];
NSString *path = nil;
NSArray *scales = [NSBundle preferredScales];
for (int s = 0; s < scales.count; s++) {
CGFloat scale = ((NSNumber *)scales[s]).floatValue;
NSString *scaledName = ext.length ? [name stringByAppendingNameScale:scale]
: [name stringByAppendingPathScale:scale];
path = [self pathForResource:scaledName ofType:ext];
if (path) break;
}
return path;
}
- (NSString *)pathForScaledResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)subpath {
if (name.length == 0) return nil;
if ([name hasSuffix:@"/"]) return [self pathForResource:name ofType:ext];
NSString *path = nil;
NSArray *scales = [NSBundle preferredScales];
for (int s = 0; s < scales.count; s++) {
CGFloat scale = ((NSNumber *)scales[s]).floatValue;
NSString *scaledName = ext.length ? [name stringByAppendingNameScale:scale]
: [name stringByAppendingPathScale:scale];
path = [self pathForResource:scaledName ofType:ext inDirectory:subpath];
if (path) break;
}
return path;
}
@end