[Webkit-unassigned] [Bug 161450] No reliable way to get a snapshot of WKWebView (macOS)

bugzilla-daemon at webkit.org bugzilla-daemon at webkit.org
Mon Oct 10 20:59:04 PDT 2016


https://bugs.webkit.org/show_bug.cgi?id=161450

--- Comment #16 from Dan <dasau at microsoft.com> ---
(In reply to comment #15)
> Is there a way to instead make existing AppKit or UIKit snapshot API and
> techniques work successfully on WKWebView, rather than adding a new
> WKWebView feature?

Thanks for taking a look Darin. I have searched for solutions using AppKit API, and it looks like other developers are having the same issue where none of the AppKit API will capture the contents of WKWebView. Many people refer to a private API solution, which is no good for store apps. With iOS UIKit, we are able to get the contents with (- (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates), but a solution does not seem to be available for Mac. The reason I was adding the API was for Mac, but to be consistent and allow for developer code reuse I am making it available for both Mac and iOS. If you know of a different method that I did not try, please let me know. Below are some examples I have tried and verified they cannot get the WKWebView snapshot on OSX, while most of them work on the legacy WebVew.

- (NSImage *)snapshotView1:(NSView *)view
{
    // WebView returns the correct content, WKWebView returns white snapshot.
    NSImage *image = [[NSImage alloc] initWithSize:view.bounds.size];
    NSBitmapImageRep *imageRep = [view bitmapImageRepForCachingDisplayInRect:view.bounds];
    [view cacheDisplayInRect:view.bounds toBitmapImageRep:imageRep];
    [image addRepresentation:imageRep];

    return image;
}


- (NSImage *)snapshotView2:(NSView *)view
{
    // WebView returns the correct content, WKWebView returns white snapshot.
    NSImage *image = [[NSImage alloc] initWithSize:view.bounds.size];
    [image lockFocus];
    CGContextRef ctx = [NSGraphicsContext currentContext].graphicsPort;
    [view.layer renderInContext:ctx];
    [image unlockFocus];

    return image;

}

- (NSImage *)snapshotView3:(NSView *)view
{
    // Returns an empty image with both WKWebView and WebView.
    [view lockFocus];
    NSImage *image = [[NSImage alloc] initWithData:[view dataWithPDFInsideRect:view.bounds]];
    [view unlockFocus];

    return image;

}

- (CGImageRef)snapshotView4:(NSView *)view
{
    // CGWindowListCreateImage can get WKWebView content, but only the visible portions in the parented window. If the view is inside a scrollview, clipped by the window, or overlapped with another view, it cannot access the full WKWebView viewport.
    CGImageRef image = CGWindowListCreateImage(CGRectNull, kCGWindowListOptionIncludingWindow, (CGWindowID)self.view.window.windowNumber, kCGWindowImageDefault);

    return image;
}

-- 
You are receiving this mail because:
You are the assignee for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.webkit.org/pipermail/webkit-unassigned/attachments/20161011/6bbc233c/attachment.html>


More information about the webkit-unassigned mailing list