0

我想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];

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

上面有什么问题?

4

1 回答 1

1

试试这样: -

- (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];
}
于 2013-09-19T14:36:55.370 回答