Re: Custom component fails to load images by name in a IBPlugin
Re: Custom component fails to load images by name in a IBPlugin
- Subject: Re: Custom component fails to load images by name in a IBPlugin
- From: "Dalzhim Dalzhim" <email@hidden>
- Date: Fri, 19 Sep 2008 17:23:19 -0400
Basically I have created a project in XCode with the Interface Builder
3.x plugin template. After that I have created a class along the lines
of the following code sample and included it in the framework target of
the project template. I have included all the necessary in the
IBPlugin target to add a new inspector to configure this class and the
class also contains all the necessary to serialize and unserialize
which works very well. Although when I enter a string in my inspector
to configure my instance of the class it fails to find the image (as
expected since the mainBundle is IB's bundle). But Chris' solution of
using [NSBundle bundleForClass:[MyComponent class]] failed as well in
both interface builder and the application which links with my new
framework. The mainBundle solution works in my application, but not in
interface builder.
Another weird thing, while testing all those possibilities I ended
up messing up my plugin it seems as interface builder won't load it at
all anymore. Even when I try to add it explicitly either by locating
the framework, or by locating an alias which points directly to the
.ibplugin file I get the error message: "The bundle « MyPlugin »
couldn't be loaded because it is broken or some resources are missing"
(translated from french).
Here is my sample code:
@interface MyComponent {
NSString* myImageName;
NSImage* myImage;
}
- (void) setImageName:(NSString*)
theImageName;
@end
@implementation MyComponent
- (void) setImageName:(NSString*)theImageName
{
[myImageName release];
myImageName = [theImageName retain];
myImage = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForImageResource:myImageName]];
}
@end
regards
-Dalzhim
(Forgot to use 'reply to all')
2008/9/19 Jonathan Hess
<email@hidden>
Hey Dalzhim -
Could you post a sample of the code you are using to load the image, and give me an idea of where it is being invoked?
Jon Hess
On Sep 19, 2008, at 12:56 PM, Dalzhim Dalzhim wrote:
Hello Chris,
I have tried looking for the image in the bundle of my custom component's class but that doesn't work in Interface Builder neither in my own application. My guess is that the bundle of my custom component is a bundle specific to my framework. A project which links against that framework remains in a separated bundle and thus looking for the image in the custom component's bundle fails when trying to use images introduced by the application.
It is true that using mainBundle must be returning Interface Builder's bundle instead of the application's bundle, but that solution works in the final application unlike the bundleForClass method, unless I have misunderstood which MyView class you were talking about. Thanks for helping me with this.
regards
-Dalzhim
2008/9/19 Chris Hanson
<email@hidden>
On Sep 19, 2008, at 10:21 AM, Dalzhim Dalzhim wrote:
I have packaged this component in a IBPlugin and designed a custom inspector to configure my custom component at design time by entering the names of the required images. However, my component fails to load the images when it is being configured in Interface Builder. It also used to fail to load when I ran the application which uses the framework which includes this ibplugin, but I already fixed that by searching for the image in the mainBundle.
When your plug-in is run under Interface Builder, +[NSBundle mainBundle] will return Interface Builder's bundle rather than your application's bundle. Your view might want to look for the resources in its bundle — e.g., the bundle returned by [NSBundle bundleForClass:[MyView class]] — for its images.
That way, when your view class is compiled into the Interface Builder plug-in it will look in the plug-in for its images, and when it's compiled into your application it will look in your application for its images. And if you move your view to a framework that's used by both your application and your Interface Builder plug-in, you can move its images along with it and they'll be found there as well.
-- Chris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
This email sent to email@hidden
_______________________________________________
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