There are many different techniques for creating an NSImage from an NSView - finally I found one that works well!
There are various techniques:
But this only draws the root view, not subviews.
But its nigh on impossible to actually get the transformations correct for adjusting to the subview co-ordinate systems.
But this only gets the visible parts of the view.
But this is quite slow and has some drawing artefacts when the resulting image is displayed on the screen.
I finally found Gerd Knops’ post which yields a good result.
- (NSImage *)imageWithSubviews { NSSize mySize = self.bounds.size; NSSize imgSize = NSMakeSize( mySize.width, mySize.height ); NSBitmapImageRep *bir = [self bitmapImageRepForCachingDisplayInRect:[self bounds]]; [bir setSize:imgSize]; [self cacheDisplayInRect:[self bounds] toBitmapImageRep:bir]; NSImage* image = [[[NSImage alloc]initWithSize:imgSize] autorelease]; [image addRepresentation:bir]; return image; }
I am combining this with Matt Gemmell’s MGViewAnimation with good results for use in alpha fading views under 10.5.
Posted Tuesday, April 21, 2009. Permalink. 1 Comments.
None yet.
I found an interesting bug in cacheDisplayInRect and focus ring drawing when the view you are imaging is enclosed in an NSScrollView/NSClipView (I'm not sure which, since I never use one without the other). In this case, if there is an active (firstResponder) control with a focus ring, the focus ring is clipped incorrectly. The view draws highlighted as normal, but the focus ring draws partially clipped. Eventually I determined that the offset of the clip was the same as the offset of the enclosing NSScrollView/NSClipView in the window.
I developed the following work around, which increases the bounds of the desired cached area by the appropriate offset. rdar://problem/6987153.
Posted Tuesday, June 23, 2009 05:38 AM by Peter N Lewis.