在NSImage内部使用NSStrings拍摄屏幕截图 [英] Adorning screenshots with NSStrings inside an NSImage
问题描述
我试图从NSView抓取屏幕截图,使用NSImage缓冲区在屏幕截图的顶部绘制一个字符串,然后保存所有内容。视图抓取是一个普通的NSPanel内容视图,托管单个NSImageView。
I'm trying to grab a screenshot from a NSView, draw a string on top of the screenshot using the NSImage buffer then saving everything. The view grabbed from is a plain NSPanel contentview that hosts a single NSImageView.
不幸的是,输出文件是我的字符串在所有的黑色背景,没有屏幕截图的标志。
Unfortunately the output file is my string on an all black background with no sign of the screenshot.
这是代码:
NSImage *screenshot = [[NSImage alloc] initWithData:[mView dataWithPDFInsideRect:[mView bounds]]];
NSString *frameCounterString = @"123";
[screenshot lockFocus];
[frameCounterString drawAtPoint:NSMakePoint(10, 10) withAttributes:nil];
[screenshot unlockFocus];
有什么想法吗?
一旦lockFocus / unlockFocus调用添加到结果中就变成黑色..
As soon as the lockFocus/unlockFocus calls are added in the result goes black..
使用NSBitmapImageRep:initWithFocusedViewRect:捕获的技术在这种情况下无效,例如:
Using the NSBitmapImageRep:initWithFocusedViewRect: technique for capturing doesn't work either in this case, i.e.:
screenshot = [[NSImage alloc] initWithSize:[mView bounds].size];
[mView lockFocus];
NSBitmapImageRep *bits = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[mView bounds]];
[mView unlockFocus];
[screenshot addRepresentation:bits];
推荐答案
正确 - 现在合成几个NSImages docs。
一个用于屏幕截图,而不是将该字符串加上一个新的空NSImage。
Right - now compositing several NSImages as suggested by the Apple docs. One for the screenshot, than drawing that plus the string into a new empty NSImage.
仍为黑色。
要捕获的视图包含一个NSImageView,它必须是关于其内部图像表示的东西:将NSImageView边框设置为none不会产生任何截图。将边框设置为Photo允许使用dataWithPDFInsideRect方法捕获边框。
The view to be captured contains a NSImageView and it must be something about its internal image representations: setting the NSImageView border to none doesn't yield any screenshot, ever. Setting the border to 'Photo' allows it to be captured using the dataWithPDFInsideRect method.
无论显示模式是什么,都需要找到一个更通用的方法来捕获它。 。
Need to find a more generic way to capture it no matter what the display mode is..
这篇关于在NSImage内部使用NSStrings拍摄屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!