From 9366a1ec508bb2f45604d5212c0709f88caeca2e Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Sun, 19 Aug 2012 14:44:57 +0200 Subject: [PATCH] Cache image resources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- Frameworks/OakAppKit/src/NSImage Additions.mm | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Frameworks/OakAppKit/src/NSImage Additions.mm b/Frameworks/OakAppKit/src/NSImage Additions.mm index 33b639fb..6ee975c7 100644 --- a/Frameworks/OakAppKit/src/NSImage Additions.mm +++ b/Frameworks/OakAppKit/src/NSImage Additions.mm @@ -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; } // ===================================================