NSScrollView

Discussion in 'Mac Programming' started by VictorTiamat, Aug 25, 2008.

  1. macrumors newbie

    Joined:
    Aug 25, 2008
    #1
    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.
     
  2. macrumors 6502

    Joined:
    Jul 25, 2006
    #2
    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.
     
  3. thread starter macrumors newbie

    Joined:
    Aug 25, 2008
    #3
    Same problem. I've got just visible part snapshot. :(
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     
  5. thread starter macrumors newbie

    Joined:
    Aug 25, 2008
    #5
    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.
     

Share This Page