如何通过程序截取屏幕截图?
UIGraphicsBeginImageContext
来实现此目的。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
,它是一个Webview,你可以用任何你想用的名字。这会获取网页视图中当前可见的屏幕截图。
2. 通过使用UIGraphicsGetImageFromCurrentImageContext()
,我将我的Webview的屏幕截图转换为图像。
3. 通过使用UIGraphicsEndImageContext()
,我结束了上下文。
4. 我将图像保存在NSData
中,因为我需要将截图通过邮件发送。如果需要发送或保存,保留在NSData
中似乎是个不错的选择。
编辑: 要将其添加到相机胶卷中,你需要编写以下代码:
UIImageWriteToSavedPhotosAlbum(theImage,nil,NULL,NULL);
添加在 UIGraphicsEndImageContext();
之后。
UIImageWriteToSavedPhotosAlbum
,请查看我的编辑。 - iNoob