Creating NSImageView programmatically [fixed]

Oldtimer
Posts: 834
Joined: 2002.09
Post: #1
I'm trying to create a splash screen by bringing up an NSWindow and putting an image on it – programmatically. I've got a good reason not to use a .nib file, so bear with me.

I get the window alright, but the image doesn't show up. I've never worked with NSImage or NSImageReps, so perhaps I'm missing something?

Code:
        splashWindow = [[NSWindow alloc] initWithContentRect: NSMakeRect (0, 0, 400, 400)
                                         styleMask: NSBorderlessWindowMask
                                        backing:NSBackingStoreBuffered
                                        defer:NO];

        NSImage *imageFromBundle = [NSImage imageNamed:@"splashscreen.png"];
        NSImageView *imageView = [[NSImageView alloc] init];
        [imageView setImage: imageFromBundle];
        [[splashWindow contentView] addSubview: imageView];
        [imageView setBounds: NSMakeRect (0, 0, 256, 256)];

        [splashWindow setHasShadow: YES];
        [splashWindow center];
        [splashWindow makeKeyAndOrderFront: self];
        [imageView display];

If anyone could point me in some sort of direction, it'd be much appreciated.

[EDIT: Fixed it, I was using [NSImageView init] instead of [NSImageView initWithFrame:].]
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
What on earth good reason do you have not to use a nib file? Rasp
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #3
OK, scratch "good" reason, but here are some:
- I'm building a custom-shaped window, so I'll use the image view to knock out the silhouette. Doesn't make sense to build a opaque NSWindow in IB, then.
- My resource handling script doesn't play well with including images in nib files, so I couldn't put the image on the window in IB.
- There'll be some custom animation done on the window, reshaping and moving it in a way that didn't make sense to setup in IB
- Worst of all: there's no way to create a borderless window in IB. Sad
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
yeah, the no-borderless-windows-in-IB thing is a good reason... though it hasn't stopped me Wink

I've created an AlwaysBorderlessWindow class that's a subclass of NSWindow, overriding the two -initWith........ methods to change the style mask. Works fine.
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #5
*snap* Should've thought of that. Lawl. Grin
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Can you programmatically set the title of a NStextfield? kensuguro 5 5,861 Sep 6, 2005 08:09 AM
Last Post: Steven