我想在主视图中显示一个NSImageView

img=[[NSImageView alloc]init];
[img setImage:[[NSImage alloc]initWithContentsOfFile:filename]];
width=img.image.size.width/2;
height=img.image.size.height/2;
[img setFrame:NSMakeRect(0,0,width ,height)];
mainview = [((AppDelegate *)[NSApp delegate]).window contentView];
[mainview addSubview:img];


图像可以正确显示,但是它完全位于屏幕的右侧,而不是位于主窗口的左上角。

上面有什么问题?

最佳答案

尝试这样:-

- (void)windowDidLoad
{
 NSString *imageName = [[NSBundle mainBundle] pathForResource:@"yourImage ofType:@"tiff"];
    NSImage *photoImage = [[NSImage alloc] initWithContentsOfFile:imageName];
    [yourimgView setImage:photoImage];
    CGFloat width=yourimgView.bounds.size.width;
    CGFloat height=yourimgView.bounds.size.height;
    CGFloat xboundspoint=yourimgView.bounds.origin.x;
    CGFloat yboundspoint=yourimgView.bounds.origin.y;
    [yourimgView setFrame:NSMakeRect(xboundspoint, yboundspoint+150.0, width, height)];
    [yourview addSubview:yourimgView];
    [[[self window]contentView]addSubview:yourview];
    [super windowDidLoad];
}

10-05 21:40