使用代码截图

我已经看了很多关于如何以编程方式截图的教程,我没有任何运气:(请有人告诉我,因为我只是编了一会儿,现在!谢谢!

你可以使用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.首先我把图像上下文,我把它作为一个webview的myView ,你可以给你任何你想要的。 这需要可以在屏幕上看到的webview的图像。

2使用UIGraphicsGetImageFromCurrentImageContext()我将我的webview的屏幕截图转换成图像。

3.通过使用UIGraphicsEndImageContext()我要求它结束上下文。

4.我将图像保存到NSData因为我不得不邮寄截图。 并保持在NSData似乎是一个很好的select,如果它是用来发送或保存。

编辑:要将其添加到相机卷,你需要写:

UIImageWriteToSavedPhotosAlbum(theImage,nil,NULL,NULL); UIGraphicsEndImageContext();

看看这个答案。它也照顾视网膜显示。

其实要解释的过程,

  • select一个图像上下文大小(可能是您需要屏幕截图的图层大小)
  • 在创build的上下文中渲染要采取截图的图层
  • 从上下文获得图像,你就完成了!