Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 60 additions & 20 deletions StandardPaths/StandardPaths.m
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,69 @@ - (NSString *)pathForResource:(NSString *)file
return [[self resourcePath] stringByAppendingPathComponent:file];
}

- (NSArray *)localisedPathsForResource:(NSString *)fileOrPath
{
NSArray *localizations = [[NSBundle mainBundle] preferredLocalizations];
NSMutableArray *paths = [[NSMutableArray alloc] initWithCapacity:localizations.count];
for (NSString *localization in localizations) {
NSString *path = [[[self resourcePath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.lproj", localization]] stringByAppendingPathComponent:fileOrPath];
[paths addObject:path];
}
NSString *path = [[[self resourcePath] stringByAppendingPathComponent:@"Base.lproj"] stringByAppendingPathComponent:fileOrPath];
[paths addObject:path];

return paths;
}

- (NSString *)normalizedPathForFile:(NSString *)fileOrPath
{
return [self normalizedPathForFile:fileOrPath ofType:@"png"];
}

- (NSString *)normalizedPathForFile:(NSString *)fileOrPath ofType:(NSString *)extension
{

//normalize extension
if (![[fileOrPath SP_pathExtension] length] && [extension length])
{
fileOrPath = [fileOrPath SP_stringByAppendingPathExtension:extension];
}

if ([fileOrPath isAbsolutePath])
{
return [self normalizedPathForAbsoluteFilePath:fileOrPath];
}

NSString *finalPath = nil;

NSString *absoluteFileOrPath = [self pathForResource:fileOrPath];
finalPath = [self normalizedPathForAbsoluteFilePath:absoluteFileOrPath];
if (finalPath)
{
return finalPath;
}

NSArray *paths = [self localisedPathsForResource:fileOrPath];
for (NSString *localisedPath in paths)
{
finalPath = [self normalizedPathForAbsoluteFilePath:localisedPath];
if (finalPath)
{
return finalPath;
}
}

return finalPath;
}


- (NSString *)normalizedPathForAbsoluteFilePath:(NSString *)absoluteFilePath
{
//set up cache
static NSCache *cache;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

cache = [[NSCache alloc] init];

#if TARGET_OS_IPHONE
Expand All @@ -273,23 +324,11 @@ - (NSString *)normalizedPathForFile:(NSString *)fileOrPath ofType:(NSString *)ex

});

//normalize extension
if (![[fileOrPath SP_pathExtension] length] && [extension length])
{
fileOrPath = [fileOrPath SP_stringByAppendingPathExtension:extension];
}

//convert to absolute path
if (![fileOrPath isAbsolutePath])
{
fileOrPath = [self pathForResource:fileOrPath];
}

@synchronized (cache)
{
//check cache
NSString *cacheKey = fileOrPath;
BOOL cachable = [fileOrPath hasPrefix:[self resourcePath]];
NSString *cacheKey = absoluteFilePath;
BOOL cachable = [absoluteFilePath hasPrefix:[self resourcePath]];
if (cachable)
{
NSString *path = [cache objectForKey:cacheKey];
Expand All @@ -300,15 +339,15 @@ - (NSString *)normalizedPathForFile:(NSString *)fileOrPath ofType:(NSString *)ex
}

//generate all possible paths
NSArray *paths = @[fileOrPath];
NSArray *paths = @[absoluteFilePath];

//check for Retina
if (!SP_IS_RETINA())
{
//insert Retina versions before non-Retina
paths = @[[fileOrPath stringByAppendingSuffixForScale:3],
[fileOrPath stringByAppendingSuffixForScale:2],
fileOrPath];
paths = @[[absoluteFilePath stringByAppendingSuffixForScale:3],
[absoluteFilePath stringByAppendingSuffixForScale:2],
absoluteFilePath];
}

switch (UI_USER_INTERFACE_IDIOM())
Expand Down Expand Up @@ -382,9 +421,10 @@ - (NSString *)normalizedPathForFile:(NSString *)fileOrPath ofType:(NSString *)ex
case UIUserInterfaceIdiomDesktop:
{
//add HiDPI tiff extension
NSString *extension = [absoluteFilePath pathExtension];
if ([@[@"", @"png", @"jpg", @"jpeg"] containsObject:[extension lowercaseString]])
{
paths = [paths arrayByAddingObject:[fileOrPath stringByReplacingPathExtensionWithExtension:@"tiff"]];
paths = [paths arrayByAddingObject:[absoluteFilePath stringByReplacingPathExtensionWithExtension:@"tiff"]];
}

//add HD suffix
Expand Down