Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

VictorTiamat

macrumors newbie
Original poster
Aug 25, 2008
3
0
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

macrumors 6502
Jul 25, 2006
294
0
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

macrumors newbie
Original poster
Aug 25, 2008
3
0
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. :(
 

VictorTiamat

macrumors newbie
Original poster
Aug 25, 2008
3
0
Problem is solved :)

My "how to":

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

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

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

Code:
- (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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.