Search code examples
objective-cmacosnsimageview

Cannot set image in NSImageView


I have a little piece of program to display an image in an Image-well. I've being testing around but there's nothing shows up on the screen. Appreciate any one can help.

.h file:

#import <Cocoa/Cocoa.h>
@interface TestAppDelegate : NSObject <NSApplicationDelegate>
{
    IBOutlet NSImageView *myImageView;
}
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSImageView *myImageView;
@end

.m file:

#import "TestAppDelegate.h"

@implementation TestAppDelegate

- (void)dealloc
{
    [super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSString * filePath = @"haha.png";  // the pic file that I added into the project
    NSImage * aImage = [NSImage imageNamed:filePath];

    if(aImage)
    {
        NSLog(aImage.name);
        NSLog(@"size %f %f",aImage.size.width,aImage.size.height);
        //both of the NSLogs work but the Image-well shows blank            
        [myImageView setImage:aImage];
    }
}
@end

Solution

  • You have to use UIImageView in place of NSImageView.