cocoansimageview

Adding a nsimageview by coding in a Mac app


I have been working on iPhone for some time and just recently I have been trying my hands on a Mac app. But it is giving me a hard time. I want to add an image over my window from coding. I have done following code but it doesn't work:

NSImageView *imView = [[NSImageView alloc] initWithFrame:NSMakeRect(250, 50, 180, 160)];
NSImage *myImage = [[NSImage alloc] initByReferencingFile:@"top.png"];
[imView setImage:myImage];
[window setContentView:imView];

Solution

  • ok I found my mistake. I was adding it to window instead I should have added it to content view. I changed my code to following and it worked:

    [self.window.contentView addSubview:imView];