From: Allison N. <dem...@ma...> - 2008-08-26 10:50:08
|
I've just finished writing a little snippet of code that made me remember why I love programming in Ruby so much, and how great it is that RubyCocoa lets me do so whilst accessing all the glory that is Cocoa :-) So I thought I'd share :-) It's an application of open classes in Ruby. Basically I wanted a method that would give me an NSImage of any NSView that I wanted. So here it is: require 'osx/cocoa' include OSX class NSView < NSResponder def captureImage bit_image_rep = self.bitImageRepForCachingDisplayInRect(self.visibleRect) self.cacheDisplayInRect_toBitmapImageRep(self.visibleRect, bit_image_rep) image = NSImage.alloc.initWithSize(bit_image_rep.size) image.addRepresentation(bit_image_rep) image end end And to use it: frame = NSRect.new(0, 0, 100, 100) some_view = NSButton.alloc.initWithFrame(frame) image = some_view.captureImage Although why you'd want to capture the image of a button is beyond me... Actually, being a bit of a Cocoa newbie, is there a simpler way of doing this? Alli |