简体   繁体   English

拍摄视图的屏幕截图

[英]Taking a screenshot of a view

How does one take a screenshot of a view with code? 如何用代码拍摄视图的屏幕截图?

I have read on some forums and there was a post made by an apple engineer and the code didnt work. 我已经在一些论坛上阅读了,有位苹果工程师发了帖,代码没有用。

Is there a built in way to do this or what is the correct way to screenshot a view? 是否有内置的方法可以执行此操作,或者对视图进行屏幕截图的正确方法是什么?

Apple just updated Technical Q&A QA1703 Screen Capture in UIKit Applications . Apple刚刚更新了UIKit Applications中的技术问答QA1703 屏幕捕获 I just put this in my code and it works well. 我只是将其放在我的代码中,并且效果很好。

You can let the layer of the view render into a CGContextRef . 您可以将视图层渲染为CGContextRef This would be something like this: 这将是这样的:

[[myView layer] renderInContext:someContext];

You can then get the UIImage representation of the context and save it or so. 然后,您可以获取上下文的UIImage表示形式并进行保存。 However, please remark that this is kind of slow as the whole layer plus it sublayers have to be rendered again into the new context. 但是,请注意,这有点慢,因为整个图层及其子图层必须再次呈现到新的上下文中。

Don't forget to include and link against QuartCore for this. 不要忘记为此包含和链接QuartCore。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM