I have an NSView subclass called StretchView. Here's what I want to do: Since the StretchView has scrollbars and is extended via those scrollbars I want to be able to grab the entire StretchView (including the area not visible on screen) and save that to a file.
// StretchView.h
#import <Cocoa/Cocoa.h>
@interface StretchView : NSView {
}
-(IBAction)saveImageButtonPushed

id)sender;
@end
// StretchView.m
#import "StretchView.h"
@implementation StretchView
//......
-(IBAction)saveImageButtonPushed

id)sender
{
NSLog(@"saveImageButtonPushed");
NSBitmapImageRep *rep;
NSData *data;
NSImage *image;
[self lockFocus];
rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[self frame]];
[self unlockFocus];
image = [[[NSImage alloc] initWithSize:[rep size]] autorelease];
[image addRepresentation:rep];
data = [rep representationUsingType: NSPNGFileType properties: nil];
//save as png but failed
[data writeToFile: @"asd.png" atomically: NO];
//save as pdf, succeeded but with flaw
data = [self dataWithPDFInsideRect:[self frame]];
[data writeToFile:@"asd.pdf" atomically:YES];
}
//......
@end
The problem is, the png file that generated is only part of the ScrollView (I tried to change the "initWithFocusedViewRect:[self frame]];" to "initWithFocusedViewRect:[self bounds]];" but in vain).
Then I tried to save the NSView content as pdf file and this time the whole view content was grabbed successfully but the backgound color was gone(I've set the content view's background as red but in the pdf file the background was white).
Can anybody give a hint, thans^_^