File: NSImageAdditions.m

package info (click to toggle)
projectmanager.app 0.2-3.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,812 kB
  • ctags: 306
  • sloc: objc: 19,267; makefile: 77; sh: 64
file content (44 lines) | stat: -rw-r--r-- 1,293 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#import "NSImageAdditions.h"

#import <Foundation/NSBundle.h>

@implementation NSImage (PMAdditions)

/**
 * Locates and initializes a named image from the bundle of an object.
 *
 * @param aName The name of the image, without an extension.
 * @param owner The object who's bundle will be searched.
 *
 * This method is more suitable for loading images from external bundles,
 * since the normal +[NSImage imageNamed:] doesn't look there.
 *
 * @return An initialized and autoreleased image object or `nil'
 *      if the image wasn't found.
 */
+ (NSImage *) imageNamed: (NSString *) aName owner: (id) owner
{
  NSBundle * bundle;
  NSString * imagePath;
  NSImage * image;

  bundle = [NSBundle bundleForClass: [owner class]];
  // try file extensions "tiff", "jpg", "jpeg", "png" and "gif"
  (imagePath = [bundle pathForResource: aName ofType: @"tiff"]) ||
    (imagePath = [bundle pathForResource: aName ofType: @"jpg"]) ||
    (imagePath = [bundle pathForResource: aName ofType: @"jpeg"]) ||
    (imagePath = [bundle pathForResource: aName ofType: @"png"]) ||
    (imagePath = [bundle pathForResource: aName ofType: @"gif"]);

  if (imagePath != nil)
    {
      return [[[NSImage alloc] initByReferencingFile: imagePath] autorelease];
    }
  else
    {
      return nil;
    }
}

@end