Cache image resources

This was also done prior to commit 806cb44. I just assumed that now that we are back using the system’s named image stuff, we would benefit from their cache, but clearly we are not (especially file browser seemed sluggish because of (re)loading the file type images).
This commit is contained in:
Allan Odgaard
2012-08-19 14:44:57 +02:00
parent 8e28856cb7
commit 9366a1ec50

View File

@@ -3,7 +3,23 @@
@implementation NSImage (ImageFromBundle)
+ (NSImage*)imageNamed:(NSString*)aName inSameBundleAsClass:(id)aClass
{
return [[NSBundle bundleForClass:aClass] imageForResource:aName];
if(!aName)
return nil;
NSBundle* bundle = [NSBundle bundleForClass:aClass];
NSString* name = [NSString stringWithFormat:@"%@.%@", [bundle bundleIdentifier], aName];
static NSMutableDictionary* cache = [NSMutableDictionary new];
if(NSImage* res = [cache objectForKey:name])
return res;
if(NSImage* image = [[NSBundle bundleForClass:aClass] imageForResource:aName])
{
[cache setObject:image forKey:name];
return image;
}
return nil;
}
// ===================================================