问题描述
如何以编程方式拍摄屏幕截图?
How to take a screenshot programmatically?
推荐答案
您可以将UIGraphicsBeginImageContext
用于此目的.
You can use UIGraphicsBeginImageContext
for this purpose.
例如:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.myView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage*theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData*theImageData = UIImageJPEGRepresentation(theImage, 1.0 ); //you can use PNG too
[theImageData writeToFile:@"example.jpeg" atomically:YES];
这里
1..首先,我获取图像上下文,将其命名为myView
,这是一个Web视图,您可以根据需要提供任何内容.这将获得可以在屏幕上看到的webview图像.
1. First i take the image context, i've given it as myView
which is a webview, you can give whatever you wish for. This takes the image of webview that can be seen on the screen.
2 我使用UIGraphicsGetImageFromCurrentImageContext()
将网络视图的屏幕截图转换为图像.
2 Using UIGraphicsGetImageFromCurrentImageContext()
i convert the screenshot of my webview into an image.
3..我要求使用UIGraphicsEndImageContext()
结束上下文.
3. By using UIGraphicsEndImageContext()
i ask it end the context.
4..我将图像保存到NSData
中,因为我必须将屏幕截图发送出去.并将其保存在NSData
中似乎是个不错的选择.
4. I'm saving the image into an NSData
because i had to mail the screenshot. And keep it in NSData
seemed a good option if it is used to send or save.
要将其添加到相机胶卷中,您需要编写:
To add it to the camera roll you need to write:
UIImageWriteToSavedPhotosAlbum(theImage,nil,NULL,NULL);
这篇关于如何在iOS上使用代码拍摄屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!