Search code examples
objective-cnsviewnsimage

Instance variable NSImage is not drawn


He, since I use my images as instance variables (cause I need to draw them all very often) in this way (in my NSView):

@implementation BeatView
- (id)initWithFrame:(NSRect)frame{
self = [super initWithFrame:frame];
if (self) {
    bpm=160;
    mbpm=0;
    NSImage *bz_BG = [NSImage imageNamed:@"mw_bg01.png"];
    NSImage *bz_PaAc = [NSImage imageNamed:@"pattactive.png"];
    NSImage *bz_PaIa = [NSImage imageNamed:@"pattinactive.png"];
}
return self;
}

and then drawn:

- (void)drawRect:(NSRect)dirtyRect{
     NSPoint imagePos = NSMakePoint(0, 0);
    [bz_BG dissolveToPoint:imagePos fraction:1.0];
}

there are no errors (only warnings that the image variables are not used). When running, no image will be drawn. What am I doing wrong here? Thanks...


Solution

  • You are not initialising your instance variables. You are creating new variables during init method, hence the warning.

    Change your code to the following

    _bz_BG = [NSImage imageNamed:@"mw_bg01.png"];
    _bz_PaAc = [NSImage imageNamed:@"pattactive.png"];
    _bz_PaIa = [NSImage imageNamed:@"pattinactive.png"];
    

    And in your header file, declare those variables as

    @property (nonatomic, retain) NSImage *bz_BG;
    @property (nonatomic, retain) NSImage *bz_PaAc;
    @property (nonatomic, retain) NSImage *bz_PaIa;
    

    By default XCode (in version 4 and later I believe) synthesises your properties with an underscore in the beginning (equivalent to putting @synthesize bz_BG = _bz_BG; at the beginning of your implementation file). Hence change your references to _bz_BG instead