ftp.nice.ch/Attic/openStep/tools/workspace/TheShelf.0.3.3.sd.tgz#/TheShelf.0.3.3.sd/Source/NSImage_MiscExtensions.m

This is NSImage_MiscExtensions.m in view mode; [Download] [Up]

/*************************************************************************
 * File Name: NSImage_MiscExtensions.m
 * Version  : 0.0 alpha
 * Date     : Sun 17-Aug-1997
 *************************************************************************
 *  COPYWHAT (C) 1997 by Tomi Engel
 *
 * This notice may not be removed from this source code.
 * The use and distribution of this software is governed by the
 * terms of the MiscKit license agreement.  Refer to the license
 * document included with the MiscKit distribution for the terms.
 *                    ALL RIGHTS RESERVED
 *
 *************************************************************************
 * Notes      :
 * Bugs       :
 * Author(s)  : tsengel
 * Last update: $Date: 1997/08/17 13:50:37 $
 * History    : $Log: NSImage_MiscExtensions.m,v $
 * History    : Revision 1.1  1997/07/23 13:50:37  tsengel
 * History    : Create classes to handle other image creation
 * History    :
 *************************************************************************/

#import "NSImage_MiscExtensions.h"

@implementation NSImage (MiscExtensions)
/*"
This category adds methods which help loading images. Remember that in order to find an image faster you can ,or even should, assign it a name via #setName:.

#NOTE: The regular  #imageNamed: has a bug since loading a name named "MyImage.tiff" assigns it the name "MyImage"...but on the next request it will not try to find the "MyImage" image. This cases you code to leak all the images since tehy are still included into the internal table.

It remains to be seen if Apple fixes #imageNamed:. In the mean time you can use our methods since they already include a workaround for that problem.
"*/



+ (id)imageNamed:(NSString *)name inContext:(id)object
/*"
    Just calls #imageNamed:inContext:searchAllBundles: with a search applied to all bundles and frameworks.
"*/
{
    return [self imageNamed:(NSString *)name inContext:(id)object searchAllBundles:YES];
}

+ (id)imageNamed:(NSString *)name inContext:(id)object searchAllBundles:(BOOL)flag
/*"
        We first ask #imageNamed: if it already knows about an image with the specified name. If there is no image the search continues in the context of object. In the case where object is a normal instance we frist check the bundle where object came from. In the case of object being an instance of NSBundle we first check that bundle.
           Only pass in the name of an image following the same rules as for the #imageNamed: method. Never use a complete path since the purpose is to find a path for an image.

        #HACK: The following is not yet implemented since we need some more code for the NSBundle class.

    If that still doesn't help we will go out and try to check with all bundles and all frameworks which have been registered up to now.
"*/
{
    id		ourImage = nil;
    id		strippedName;
    id		aPath;
    id		aBundle;
    id		imageTypes;
    id		aType;
    id		enumerator;

    // First check if it is already know by name

    ourImage = [self imageNamed:name];
    if( ourImage ) return ourImage;

    // <<HACK>> The following is a workaround to a problem the "imageNamed:" method is having
    // if the image name matches that of a file...we'll check to see if the base filename
    // Since the setName is using that method too.
    // This part should be removed once imageNamed: has been fixed.
    // Even if this is a workaround..we ned the strippedName later anyway..so its not too bad.

    strippedName = [name stringByDeletingPathExtension];
    imageTypes = [NSImage imageUnfilteredFileTypes];

    if( [imageTypes containsObject:[name pathExtension]] )
    {
        ourImage = [self imageNamed:strippedName];
        if( ourImage ) return ourImage;
    }
    // Now check in the context bundle

    if( [object isKindOfClass:[NSBundle class]] )
        aBundle = object;
    else
        aBundle = [NSBundle bundleForClass:[object class]];

    // Now ask for the image resources. This will automatically check for all supported image types.

    aPath = [aBundle pathForImageResource:name];
    if( aPath )
    {
        // Seems like we have a valid path...just load, set the name and get going !
        // We will always use teh stripped name to conform with imageNamed: behavior.

        ourImage = [[NSImage alloc] initWithContentsOfFile:aPath];
        [ourImage setName:strippedName];
        return [ourImage autorelease];
    }


    // Seems like we really need to check all bundles :-( )

    // <<HACK>> Code missing here...we nned to find a smooth way to learn about all loaded bundles.
    // Maybe the MiscBundle class could help here isnce the following only works on 4.x  not 4.0 etc. 
    // + (NSArray *)allBundles;
    // + (NSArray *)allFrameworks;
    //  Maybe we should use the MiscBundle and add these methopds on platforms where they are not supported ??

    // This will become available once we have a useful extension of NSBundle..something like
    // [NSBundle pathForResource:ofType:]
    // [NSBundle pathForFrameworkResource:ofType:]  ... usesAllFrameworks internally
    // [NSBundle pathForBundleResource:ofType:]  ... uses allBundles internally

    return nil;
}

@end

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.