NSImage disappears
I have a class named ImageFrames that has two methods like this:
And then I use it like so:
I always get the message from -imageAtFrame that the image is null. I have tried using initWithContentsOfFile instead of imageNamed but it still doesn't work. Why?
Code:
// In interface
NSImage *imageCache[2];
// in implementation
- (void)loadImage:(NSString *)path forFrame:(int)frame
{
imageCache[frame] = [[NSImage imageNamed:path] retain];
[imageCache[frame] setDataRetained:YES];
[imageCache[frame] lockFocus];
[imageCache[frame] unlockFocus];
if(imageCache[frame] == NULL)
{
NSLog(@"loadImage: image is null");
}
}
- (NSImage *)imageAtFrame:(int)frame
{
if(imageCache[frame] == NULL)
{
NSLog(@"frame num %d is null", frame);
}
return(imageCache[frame]);
}Code:
// in interface
ImageFrames *test;
// in awakeFromNib
test = [[ImageFrames alloc] init];
// in initWithFrame
[test loadImage:@"testImage" forFrame:1];
// in drawRect
[[test imageAtFrame:1] compositeToPoint:NSMakePoint(10, 10) operation:NSCompositeSourceOver];
Make sure the image is getting copied to your Resources folder during the build. Have you tried stepping through this with the debugger? That might shed some light on the issue.
Yes, the image is in the appropriate folder and in the bundle. When I go through with the debugger, the image is there, but then, when it leaves the -loadImage:forFrame: it disappears. As if it hadn't been retained. I tried calling -retain on it twice, but still nothing. If I put this in the -init method, then isa is not null, but everything else still is.
This is driving me crazy, in case you couldn't tell. In theory, this should work...
Code:
frameCache[0] = [[NSImage alloc] init];
frameCache[1] = [[NSImage alloc] init];
Have you tried converting your array to an NSArray object? I doubt this would make a difference but it's something else to try...
Let me see if I understand the whole of your code: in awakeFromNib, you alloc/init the instance, but in initWithFrame: you load the image into the instance, which won't be created until awakeFromNib?
Ah! That was the problem. I was under the impression that awakeFromNib was called before initWithFrame. Now that I know, it works fine
jnikolai, I actually starting with an NSArray but when I had trouble I thought that might be the source of it, so it got axed.
jnikolai, I actually starting with an NSArray but when I had trouble I thought that might be the source of it, so it got axed.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| NSImage leaking? | Cirdan | 5 | 3,261 |
Nov 16, 2009 12:02 PM Last Post: SethWillits |
|
| Sending an NSImage on a Network | mindwalkernine | 1 | 2,760 |
Aug 9, 2006 04:23 AM Last Post: djork |
|
| Animate An NSImage | mindwalkernine | 1 | 3,549 |
Jun 18, 2006 12:30 AM Last Post: StealthyCoin |
|
| NSImage and NSView problems | Joseph Duchesne | 1 | 3,281 |
Aug 19, 2005 07:36 AM Last Post: unknown |
|
| Faking a Z Coordinate With NSImage | Nick | 2 | 2,559 |
Jun 11, 2005 05:38 PM Last Post: Nick |
|

