Support loading @2x image assets

Untested, but we now use imageForResource: (10.7) instead of constructing an absolute file path, so the system should take care of finding @2x assets.
This commit is contained in:
Allan Odgaard
2012-08-19 01:31:02 +02:00
parent 4c66bfd213
commit 806cb44b4b

View File

@@ -3,35 +3,7 @@
@implementation NSImage (ImageFromBundle)
+ (NSImage*)imageNamed:(NSString*)aName inSameBundleAsClass:(id)aClass
{
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;
NSString* resourcePath = bundle.resourcePath;
for(NSString* resource in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:resourcePath error:nil])
{
static NSSet* imageTypes = [[NSSet alloc] initWithArray:[NSImage imageFileTypes]];
if([aName isEqualToString:[resource stringByDeletingPathExtension]] && [imageTypes containsObject:[resource pathExtension]])
{
NSString* imagePath = [resourcePath stringByAppendingPathComponent:resource];
if(NSImage* image = [[[NSImage alloc] initWithContentsOfFile:imagePath] autorelease])
{
[cache setObject:image forKey:name];
return image;
}
}
}
NSBeep();
NSLog(@"*** could not find image named %@ in %@", aName, resourcePath);
return nil;
return [[NSBundle bundleForClass:aClass] imageForResource:aName];
}
+ (NSImage*)imageWithCGImage:(CGImageRef)cgImage