1

我有一个小程序可以在图像井中显示图像。我正在测试,但屏幕上没有任何显示。感谢任何人都可以提供帮助。

.h 文件:

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

.m 文件:

#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
4

4 回答 4

2

代替

NSImage * aImage = [NSImage imageNamed:filePath]

NSImage * aImage = [[NSImage alloc] initWithContentsOfFile:filePath];
于 2013-06-18T08:52:38.220 回答
0

可能是在应用程序的捆绑包中找不到 png 文件。但令人困惑的是 NSlog 函数('NSLog(aImage.name) 和 NSLog(@"size %f %f",aImage.size.width,aImage.size.height)')。正在工作和反馈。

这是我添加png图像的方法。我创建了一个名为“资源”的新组文件夹,然后右键单击并添加准备好的照片。不知道我在这里做错了什么。

我终于弄明白了。只是暗示在“myImageView set Image:aImage”前面添加了一个“self”。像 [self.myImageView setImage:aImage];

于 2013-06-19T00:54:28.500 回答
0

我也有类似的问题,我在Assets.xcassets文件夹中添加了图像,但图像不会出现。这个问题原来是格式问题,我已经指定box.png为图像导出为PNG图像。但是我的 Mac 应用程序无法识别它(无论出于何种原因)。所以我.png从字符串名称中删除了:

[NSImage imageNamed:@"box"]

由于某种原因,我制作的图像未被识别为PNG图像。不过没关系,因为根据Apple dev docs,您不需要指定PNG图像的文件格式:

对于 PNG 图像,您可以省略文件扩展名。对于所有其他文件格式,始终包含文件扩展名。

于 2017-07-11T09:39:41.980 回答
-9

您必须使用 UIImageView 代替 NSImageView。

于 2013-06-18T08:44:50.610 回答