Re: Interface Builder and NSImage loading
Re: Interface Builder and NSImage loading
- Subject: Re: Interface Builder and NSImage loading
- From: RICKY SHARP <email@hidden>
- Date: Mon, 07 Jan 2008 04:23:19 -0800
On Sunday, January 06, 2008, at 11:21PM, "Steve Israelson" <email@hidden> wrote:
>I have made a custom button class that loads a named NSImage to
>display the button.
>I have gone through the process to create a palette for the new
>interface builder.
>The problem I am having is getting my custom button to draw properly
>within IB.
>I am guessing the problem is that the named image can not be found.
>
>I can see that if I take one of those images from the Library/Media
>palette and drag it into my UI that the NSImageWell can find and
>display it properly.
>In fact, I see it has a cool popup menu that lists all the images in
>the project's resources.
>
>So, 2 questions really:
>1) Why can't my drawing code find those named image files?
>2) How can I make a popup menu item for specifying an image with my
>custom IB plugin?
You use the words 'palette' and 'plugins' here. I'm going to assume you are creating an IB 3.x plugin?
Having said that, images are indeed tricky. In IB 2.x, one could gain access to all images available in the currently opened Xcode project. Such images were also available at both design time and when testing the interface. Apple's new plugin solution only works at design time and not when running via the new simulator.
The solution I came up with was to create a separate image "bundle" that would hold a copy of the project images. Here is what I currently have (names changed to simply things):
(1) IIWidgets.framework (my main widgets plugin used by all my apps). Contained within is IIWidgets.ibplugin. The framework is installed to ~/Library/Frameworks
(2) IIAppOneWidgets.framework, IIAppTwoWidgets.framework, etc. Each app also needed a custom plugin for widgets unique to that app. These also contain the corresponding .ibplugins and also are installed to ~/Library/Frameworks
(3) Xcode project folders for each of my apps. In each folder is an "Images" folder which contains all images I want to place in the app's Resources folder. Projects here are named IIAppOne, IIAppTwo, etc.
(4) I build up dummy "image" bundles for each of my apps named IIAppOneImages.bundle, IIAppTwoImages.bundle, etc.
I then created a "populate images"*** application and placed a copy in each of my Xcode project folders. This app walks the folder hierarchy it's in to gather the project name (e.g. IIAppOne) and make a copy of all the project's images into the appropriate image .bundle.
Finally, I have an "image factory" class as part of each widgets framework. When accessed in the context of IB, the image factory behaves differently (I set up a special prefix #define in my plugin's prefix header). When I build the factory singleton within the context of IB, it scans ~/Library/Frameworks for patterns matching II*Images.bundle and builds up an array of NSBundle instances. Since I'm only accessing images from those bundles, I didn't nead to load the bundles. The code to load images then ends up like this:
if requested image name is found in bundle #1, then return it
else if requested image name is found in bundle #2, etc.
Note, however, that this model requires uniquely named images. I hope to one day figure out how to sense the currently opened Xcode project, and use its name to determin which image bundle to load images from.
Whenever I add an image to my app project's Images folder, I re-run my "populate images" app and that image is made available to me within IB.
*** the "populate images" app actually does a bit more stuff. I used it to also encrypt my images. Encrypted images ultimately go into the built app. Uncrypted copies though are placed into my image bundles for use in IB.
I have also not yet added images to a combo box for use within IB. One could extend my solution and dynamically populate a combo box with images found in the images bundle(s).
--
Rick Sharp
Instant Interactive(tm)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden