如何采取使用iOS上的代码截图?(How to take a screenshot using co

2019-07-28 22:39发布

如何采取截图编程?

Answer 1:

您可以使用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是网页视图,你可以给任何你希望的。 这需要web视图的图像可以在屏幕上看到。

2使用UIGraphicsGetImageFromCurrentImageContext()我在我的网页视图的屏幕截图转换为图像。

3.使用UIGraphicsEndImageContext()我问什么时候结束的背景下。

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

编辑:将它添加到你需要写的相机胶卷:

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



Answer 2:

看看这个答案 。它还采用Retina显示屏的照顾。

其实解释的过程中,

  • 选择一个图像上下文大小(可能是您需要的屏幕截图层大小)
  • 渲染要采取截图在创建的上下文层
  • 获得从上下文的形象和你做!


文章来源: How to take a screenshot using code on iOS?