【问题标题】:NSImage property always return null?NSImage 属性总是返回 null?
【发布时间】:2013-07-13 16:52:10
【问题描述】:

我有一个名为电影的自定义类。很简单,只有三个属性:

.h

@interface WTAMovie : NSObject

@property (strong, nonatomic) NSImage *theImage;
@property (strong, nonatomic) NSString *theTittle;
@property (strong, nonatomic) NSString *theBlurb;


@end

.m 有一个初始化器,它为属性设置一些默认值,如果记录自定义对象,则返回属性值的描述方法。像这样。

-(NSString *)description
{
NSString *desc = [NSString stringWithFormat:@"<Movie> - theImage = %@, theTittle = %@,     theBlurb = %@", _theImage, _theTittle, _theBlurb];
return desc;
}

-(id)init
{

self = [super init];
if (self) {

    _theTittle = @"New Tittle";
    _theBlurb = @"Empty Blurb";


    NSString* imageName = [[NSBundle mainBundle] pathForResource:@"reel" ofType:@"png"];
    _theImage = [[NSImage alloc] initWithContentsOfFile:imageName];


    NSLog(@"from the person calss init: %@", _theImage);

}

return self;
}

在我的 Document.m 文件初始化程序中,我正在创建一个新的 Movie 对象,将其放入定义为保存电影对象的数组中,然后记录该对象。像这样:

- (id)init
{
self = [super init];
if (self) {
    // Add your subclass-specific initialization here.
    _movies = [NSMutableArray new];
    WTAMovie *firstMovie = [WTAMovie new];
    [_movies addObject:firstMovie];
    for (id object in _movies)
    {

        NSLog(@"%@", object);

    }

}
return self;
}

控制台的输出是:

2013-07-15 13:47:57.174 HomeworkFour[5044:303] - theImage = (null), theTittle = New Title, theBlurb = Empty Blurb

图像总是说它是空的?我不明白。如您所见,我是从添加到项目中的文件中设置它的????

【问题讨论】:

  • 快速浏览一下,汤米,我认为您的房产被抬高了。这是对属性属性的快速参考。 stackoverflow.com/questions/9859719/…
  • 你试过用 UIImage 代替 NSImage 吗?
  • 它将始终显示为空。您唯一可以分配的是 name 属性,如果未分配,它将显示 nil。但是图像被加载在那里。它只是不会在没有被分配的情况下显示名称。加载图像后,它不再了解其资源。

标签: properties nsimage initwithcontentsoffile


【解决方案1】:

好的,这最终变得愚蠢。我的图像资源是 .jpeg,在我的: [[NSBundle mainBundle] pathForResource:@"reel" ofType:@"png"];

我显然给出了 png 的 ofType。

尽管有一条评论,当登录到控制台时,NSImage 并非总是在描述符中显示 null。一旦 NSImage 在注销时正确初始化,该对象将显示有关该对象的几条信息,例如内存位置和某些屏幕绘图参数。

【讨论】:

    猜你喜欢
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2016-11-02
    • 2014-05-06
    相关资源
    最近更新 更多