cacheDisplayInRect如何在内部工作?

时间:2015-12-03 13:05:25

标签: objective-c swift cocoa wkwebview

将位图数据写入bitmapImageRep时,[NSView cacheDisplayInRect:(NSRect)rect toBitmapImageRep:(NSBitmapImageRep *)bitmapImageRep]如何在内部工作?

E.g。它是否遍历并访问所有子视图的CALayer layer并在调用...cacheDisplayInRect...时将它们组合在一起,或者只是将其写入自己的图层(然后该图层已包含所有子视图的表示数据) )。

我的问题的背景:我使用的本机控件(WKWebView)在截图(example SO question)时被破坏了,而不是重新发明轮子来截取整个应用程序包括WKWebView我想修复Cocoa用于位图表示的方法。

1 个答案:

答案 0 :(得分:0)

我做了一些试验和错误,并在内部找出了... cacheDisplayInRect ...和... drawInContext ...调用 - [NSView drawRect:]来渲染元素。

- [CALayer drawInContext:context]调用堆栈

#0  0x000000010000e28c in -[DXSecureWKWebView drawRect:] at /Users/x/dev/X/XSecureWKWebView.m:34
#1  0x00007fff8d386749 in -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inGraphicsContext:CGContext:topView:shouldChangeFontReferenceColor:] ()
#2  0x00007fff8d386afe in -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inGraphicsContext:CGContext:topView:shouldChangeFontReferenceColor:] ()
#3  0x00007fff8d386afe in -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inGraphicsContext:CGContext:topView:shouldChangeFontReferenceColor:] ()
#4  0x00007fff8d386168 in __46-[NSView(NSLayerKitGlue) drawLayer:inContext:]_block_invoke ()
#5  0x00007fff8d385e11 in -[NSView(NSLayerKitGlue) _drawViewBackingLayer:inContext:drawingHandler:] ()
#6  0x00007fff8d385493 in -[NSView(NSLayerKitGlue) drawLayer:inContext:] ()

- [NSView cacheDisplayInRect:toBitmapImageRep:]调用堆栈

#0  0x000000010000e2dc in -[XSecureWKWebView drawRect:] at /Users/x/dev/x/XSecureWKWebView.m:34
#1  0x00007fff8d386749 in -[NSView(NSInternal) _recursive:displayRectIgnoringOpacity:inGraphicsContext:CGContext:topView:shouldChangeFontReferenceColor:] ()
#2  0x00007fff8d65fe1c in __50-[_NSViewBackingLayer _renderForegroundInContext:]_block_invoke ()
#3  0x00007fff8d385e11 in -[NSView(NSLayerKitGlue) _drawViewBackingLayer:inContext:drawingHandler:] ()
#4  0x00007fff8d50b740 in -[_NSViewBackingLayer _renderForegroundInContext:] ()
#5  0x00007fff934f5ce1 in -[CALayer renderInContext:] ()
#6  0x00007fff934f7aaf in -[CALayer _renderSublayersInContext:] ()
#7  0x00007fff8d50b807 in -[_NSViewBackingLayer _renderSublayersInContext:] ()
#8  0x00007fff934f5cf0 in -[CALayer renderInContext:] ()
#9  0x00007fff934f7aaf in -[CALayer _renderSublayersInContext:] ()
#10 0x00007fff8d50b807 in -[_NSViewBackingLayer _renderSublayersInContext:] ()
#11 0x00007fff934f5cf0 in -[CALayer renderInContext:] ()
#12 0x00007fff934f7aaf in -[CALayer _renderSublayersInContext:] ()
#13 0x00007fff8d50b807 in -[_NSViewBackingLayer _renderSublayersInContext:] ()
#14 0x00007fff934f5cf0 in -[CALayer renderInContext:] ()
#15 0x00007fff934f7aaf in -[CALayer _renderSublayersInContext:] ()
#16 0x00007fff8d50b807 in -[_NSViewBackingLayer _renderSublayersInContext:] ()
#17 0x00007fff934f5cf0 in -[CALayer renderInContext:] ()
#18 0x00007fff934f7aaf in -[CALayer _renderSublayersInContext:] ()
#19 0x00007fff8d50b807 in -[_NSViewBackingLayer _renderSublayersInContext:] ()
#20 0x00007fff934f5cf0 in -[CALayer renderInContext:] ()
#21 0x00007fff934f7aaf in -[CALayer _renderSublayersInContext:] ()
#22 0x00007fff8d50b807 in -[_NSViewBackingLayer _renderSublayersInContext:] ()
#23 0x00007fff934f5cf0 in -[CALayer renderInContext:] ()
#24 0x00007fff8d50b11d in -[NSView _layerBackedDisplayRectIgnoringOpacity:inContext:isRootView:flipContextIfNeedeed:] ()
#25 0x00007fff8d50ac74 in -[NSView displayRectIgnoringOpacity:inContext:] ()
#26 0x00007fff8d65f685 in __62-[NSView cacheDisplayInRect:toBitmapImageRep:includeSubviews:]_block_invoke ()
#27 0x00007fff8d65f3dc in __36-[NSBitmapImageRep _captureDrawing:]_block_invoke ()
#28 0x00007fff8d65f14c in -[NSBitmapImageRep _captureDrawing:] ()
#29 0x00007fff8d65f079 in -[NSView cacheDisplayInRect:toBitmapImageRep:includeSubviews:] ()