【问题标题】:Cocoa NsView doesn't show the image可可 NsView 不显示图像
【发布时间】:2012-05-23 16:18:04
【问题描述】:

我遇到了一个我无法解决的问题。我正在尝试在我的应用中显示一个小头像。 我创建了一个 NSView 类,然后在界面生成器中添加一个自定义视图,并将其链接到该类。

它没有给我任何错误,但它不显示图像!

这是我的 .m NSView 类文件:

    #import "Avatar.h"

@implementation Avatar

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSRect rect = NSMakeRect(10, 10, 400, 300);
        imageView = [[NSImageView alloc]initWithFrame:rect];
        [imageView setImageScaling:NSScaleToFit];
        NSImage *theImage = [NSImage imageNamed:@"/Users/blabla/nonloso/prova.jpg"];
        [imageView setImage:theImage];
        [self addSubview:imageView];
    } 
    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
}

@end

这是我的 .h NSView 类文件:

#import <Cocoa/Cocoa.h>

@interface Avatar : NSView {
@private 
    NSImageView *imageView;
}
@end

【问题讨论】:

    标签: cocoa nsview nsimage


    【解决方案1】:

    [-NSImage imageNamed] 不能这样工作 - 请参阅 its documentation

    您可能需要-initWithContentsOfFile:

    【讨论】:

    • 在调用-initWithContentsOfFile: 之后theImage 是非零吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多