0

I want to take a screenshot when a webview finished rending.The follow is my code:

-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
    if (frame != [sender mainFrame]) {
    return;
}
    NSBitmapImageRep *rep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:[sender bounds]] autorelease];

if (rep){
    NSImage *img = [[NSImage alloc] initWithData:[rep TIFFRepresentation]];
    NSData* imgData = [img TIFFRepresentation];

    NSArray* deskTopArrayPaths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
    NSString* deskTopPath = [deskTopArrayPaths objectAtIndex:0];
    NSString* pngPath = [NSString stringWithFormat:@"%@/SaveWebPage.png",deskTopPath];
    [[NSFileManager defaultManager] createFileAtPath:pngPath contents:imgData attributes:nil];
}
}

The normal image is that:

enter image description here

But my image is this:

enter image description here

Anyone tell me why and how could i improve this situation! Thank u!

gohamgx
  • 273
  • 2
  • 16
  • a) did you enable Java, JavaScript and Plugins on the WebView? – w-m Feb 25 '13 at 08:16
  • b) when you display the WebView on your screen, is the content already missing? Or is it lost in the process of saving the image? – w-m Feb 25 '13 at 08:17
  • All enable.It looks like that the content is exist.I guess it is lost in the process of saving.How to solve? – gohamgx Feb 25 '13 at 09:28
  • The problem has been solved! The answer is [here](http://stackoverflow.com/questions/11948241/cocoa-how-to-render-view-to-image). – gohamgx Mar 08 '13 at 02:04

0 Answers0