Give up the WKWebView screen in NSImage

I tried to do an off-screen WKWebView in an image using

  • func cacheDisplayInRect(rect: NSRect, toBitmapImageRep bitmapImageRep: NSBitmapImageRep)
  • and func drawLayer(layer: CALayer, inContext ctx: CGContext)

without success. The resulting image is always empty (white or transparent). Could anyone do this in Yosemite?

+5
source share
1 answer

You can do this with drawViewHierarchyInRect: none of the other methods work, use it like this:

 UIGraphicsBeginImageContextWithOptions(newRect.size, YES, 0); BOOL ok = [view drawViewHierarchyInRect:newRect afterScreenUpdates:YES]; if (!ok) { NSLog(@"Problem with drawView..."); } UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

I do the same in iOS, but unfortunately this method is slow, it should also be run in the main thread and only works if afterScreenUpdates is set to yes. See This Answer: How to take a snapshot of a UIView that is not showing?

There is also no way to tell what I see if any aspect of the web page needs to be redrawn.

+1
source

All Articles