Search code examples
rubycocoansviewnsimagemacruby

NSView with NSImage inside of it -- how come NSImage doesn't move with the view which is its parent?


I am writing a simple MacRuby app. I have an NSView that draws an image inside of itself when its drawRect() is called.

Inside that NSView subclass my drawRect() has (among some other lines of code)

image = NSImage.imageNamed("mini_browser")
image.drawInRect( self.bounds, fromRect: self.frame, operation:NSCompositeSourceOver, fraction:1.0)

If I move my subclassed NSView via my_view.setFrameOrigin([x,y]) where x and y are some values, I get something like below. Any insight as to why this is happening? It seems almost as if the NSImage is not moving with the NSView that encloses it.

See and example here..


Solution

  • You're doing it wrong. From the documentation, the second parameter specifies:

    The source rectangle specifying the portion of the image you want to draw.
    The coordinates of this rectangle must be specified using the image's own
    coordinate system. If you pass in NSZeroRect, the entire image is drawn.
    

    So you probably want to pass in NSZeroRect as the second parameter.