我无法捕获WKWebView的屏幕截图

Vul*_*kan 5 iphone objective-c uiview ios wkwebview

我正在尝试捕获WKWebView的屏幕快照,但是我的方法无法正常工作,它返回纯色,好像图层树为空,而它似乎可以在其他视图上工作。

- (UIImage *)screenshot
{
    UIImage *screenshot;

    UIGraphicsBeginImageContext(self.frame.size);

    [self.layer renderInContext:UIGraphicsGetCurrentContext()];

    screenshot = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return screenshot;
}
Run Code Online (Sandbox Code Playgroud)

use*_*021 4

这个Stackoverflow 答案在 iOS 8 上用 WKWebView 为我解决了这个问题,其中[view snapshotViewAfterScreenUpdates:][view.layer renderInContext:]返回纯黑色或白色:

UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
UIImage* uiImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)