• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Images in custom IB palette
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Images in custom IB palette


  • Subject: Re: Images in custom IB palette
  • From: Christopher Skogen <email@hidden>
  • Date: Wed, 22 Sep 2004 11:14:19 -0500

thanks for the great response. i was on the right track, but had yet to discover all the IB interfaces. this will save me a ton of time.

enjoy the rest of your vacation,

chris



On Sep 22, 2004, at 8:38 AM, Ricky Sharp wrote:


On Sep 22, 2004, at 3:36 AM, Christopher Skogen wrote:

ricky,

i was scanning the archives and i found your issue, but no responses. i'm having the same problem as we speak. did you ever find anything out re: this? i think it has something to do with what "imageNamed" thinks is the current bundle, but i'm not sure how to realize that my view is running in IB. (or if i even need to).

I was away for a while, so never had the chance to follow up on this thread. I'm still on vacation, so any further replies may not come right away.


But the good news is that there is indeed a solution. Someone was kind enough to share some code off-list and I've since modified it a bit to suit my needs.

You basically split your code in two pieces:

(1) I came up with a simple "image factory" class that offers this single API:

+ (NSImage*)imageNamed:(NSString*)inName fromBundle:(NSBundle*)inBundle
{
NSImage* image = nil;

if ((inName != nil) && ([inName length] > 0))
{
image = [NSImage imageNamed:inName];

if ((image == nil) && (inBundle != nil))
{
NSString* imagePath = [inBundle pathForImageResource:inName];

if ((imagePath != nil) && ([imagePath length] > 0))
{
image = [[[NSImage alloc] initWithContentsOfFile:imagePath] autorelease];
}
}
}

return image;
}



In my custom cell class, whenever I need an image, I do something like this:


NSBundle* imageBundle = [NSBundle bundleForClass:[IIButtonCell class]];
NSImage* image = [IIImageFactory imageNamed:[self someMethodToObtainImageName] fromBundle:imageBundle];



(2) In the ok: method of my IB inspector class, I first call my factory to see if an image exists with a particular name. If no image exists, I call a loadProjectImage: method (code below) which also lives in my IB inspector class. The snippet of code for my ok: is


	IIButton*	aButton = [self object];
	...

else if (sender == mImageNameNormalOff)
{
[aButton setImageName:[mImageNameNormalOff stringValue] forIndex:IIButtonImageIndexNormalOff];


NSImage* image = [IIImageFactory imageNamed:[mImageNameNormalOff stringValue] fromBundle:[NSBundle bundleForClass:[IIButtonCell class]]];

		if (image == nil)
			{
			[self loadProjectImage:[mImageNameNormalOff stringValue]];
			}
		}

Here, mImageNameNormalOff is an IBOutlet (NSTextField).


Finally, here is the loadProjectImage: method which uses various IB APIs to look for images in the nib's parent project (if any):


- (void)loadProjectImage:(NSString*)inName
{
id<IBDocuments> currentDocument = [self inspectedDocument];

if (currentDocument != nil)
{
id<IBProjects> currentProject = [currentDocument project];

if (currentProject != nil)
{
NSArray* resources = [currentProject filesForFileType:IBProjectResourcesFileType];

if (resources != nil)
{
NSEnumerator* enumerator;
id element;
NSString* elementString;
NSRange range;
NSString* nameKey = [NSString stringWithFormat:@"/%@.", inName];
NSImage* image = nil;

enumerator = [resources objectEnumerator];

while ((element = [enumerator nextObject]) != nil)
{
elementString = [element path];
range = [elementString rangeOfString:nameKey];

if (range.location != NSNotFound)
{
image = [[NSImage alloc] initWithContentsOfFile:elementString];

if (image != nil)
{
[image setName:inName];
}
}
}
}
}
}
}



Basically, the loadProjectImage: will only be used when authoring in IB. When you run your app, all images are ultimately stored in its bundle and therefore can be accessed by the factory method.


Finally, I believe the only limitation of the code is when dealing with localized images. For my needs, all my images are locale-neutral. I composite in text at run-time (which is localized) into the various images to create, for example, buttons. For localized images, I would assume you'd need to beef up loadProjectImage: to only iterate over project paths in the particular locale you are working with.

___________________________________________________________
Ricky A. Sharp         mailto:email@hidden
Instant Interactive(tm)   http://www.instantinteractive.com



_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Re: Images in custom IB palette (From: Christopher Skogen <email@hidden>)
 >Re: Images in custom IB palette (From: Ricky Sharp <email@hidden>)

  • Prev by Date: Re: [NSCursor set] not setting the cursor
  • Next by Date: Re: Newbie question: autorelease sends autorelease
  • Previous by thread: Re: Images in custom IB palette
  • Next by thread: Program Crash
  • Index(es):
    • Date
    • Thread