如何以编程方式截取屏幕截图?
你可以使用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()
,我要求它结束背景。
我将图像保存到NSData
,因为我必须邮寄截图。如果它用于发送或保存,保持在NSData
似乎是一个很好的选择。
编辑:要将其添加到相机胶卷,您需要写:
UIImageWriteToSavedPhotosAlbum(theImage,nil,NULL,NULL);
之后的UIGraphicsEndImageContext();