How to take a screenshot when a webview finished r

2019-03-03 20:06发布

问题:

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:

But my image is this:

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