PDA

View Full Version : NSScrollView




VictorTiamat
Aug 25, 2008, 05:41 AM
Hello,

I have some problem with NSScrollView. I need to make a snapshot of full NSScrollView content using NSImage, but I can only get a snapshot of visible part of content. How can I get a full content snapshot? Can you help me?

Thank you.

Best regards,
Victor.



kpua
Aug 25, 2008, 09:31 AM
Take a look at -[NSView bitmapImageRepForCachingDisplayInRect:] and -[NSView cacheDisplayInRect:toBitmapImageRep:].

-[NSBitmapImageRep initWithFocusedViewRect:] (which I suspect you're using) takes its data directly from the window backing store, so there's no way to capture what's not visible.

VictorTiamat
Aug 27, 2008, 06:28 AM
Take a look at -[NSView bitmapImageRepForCachingDisplayInRect:] and -[NSView cacheDisplayInRect:toBitmapImageRep:].

-[NSBitmapImageRep initWithFocusedViewRect:] (which I suspect you're using) takes its data directly from the window backing store, so there's no way to capture what's not visible.

Same problem. I've got just visible part snapshot. :(

robbieduncan
Aug 27, 2008, 06:35 AM
Same problem. I've got just visible part snapshot. :(

Which view are you cacheDisplayInRect: on? If it's the scrollview you will only ever get the visible part of the contained NSClipView. You need to call it on the actual "data" view.

VictorTiamat
Aug 27, 2008, 07:30 AM
Problem is solved :)

My "how to":

First, I've made a subscription on WebView notification called "WebViewProgressFinishedNotification"


[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(webViewLoadFinished:)
name:WebViewProgressFinishedNotification
object:nil];


Next. I've made a method which respond to that notification:


- (void)webViewLoadFinished:(NSNotification *)notification
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
NSString *downloadDirectory = [paths objectAtIndex:0];

WebView * webView = (WebView *)[notification object];
WebFrame * frame = [webView mainFrame];
WebFrameView * view = [frame frameView];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSBitmapImageRep *imageRep = [[view documentView]
bitmapImageRepForCachingDisplayInRect:[[view documentView] frame]];
[[view documentView] cacheDisplayInRect:[[view documentView] frame] toBitmapImageRep:imageRep];

NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(1280, 1024)];
[image addRepresentation:imageRep];


NSData * TIFFData = [image TIFFRepresentationUsingCompression:NSTIFFCompressionJPEG factor:0.0];
NSString * path = [NSString stringWithFormat:@"%@/%@", downloadDirectory, @"web.jpg"];
[TIFFData writeToFile:path atomically:YES];

[pool release];
}


That works fine and gives me a full webview content snapshot.