拍摄 UIWebView 的快照/屏幕截图

Posted

技术标签:

【中文标题】拍摄 UIWebView 的快照/屏幕截图【英文标题】:Take snapshot / screenshot of UIWebView 【发布时间】:2010-08-11 12:14:34 【问题描述】:

如何拍摄UIWebView 控件的快照/屏幕截图?

【问题讨论】:

【参考方案1】:

接受的答案是正确的,但是屏幕截图不是视网膜分辨率。 使用UIGraphicsBeginImageContextWithOptions 并将其缩放参数设置为 0.0f 使其以原始分辨率捕获。

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];   
UIImage *capturedScreen = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

【讨论】:

【参考方案2】:

使用这个:

UIGraphicsBeginImageContext(rect.size);
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

【讨论】:

截取网页视图时,只截取可见区域的截图。不考虑隐藏区域。【参考方案3】:

使用此代码截取屏幕截图并将其保存到库中

    UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);

您可以通过更改屏幕截图部分来更改

UIGraphicsBeginImageContext(self.view.bounds.size);

到你各自的价值 试试吧,对你有帮助

【讨论】:

以上是关于拍摄 UIWebView 的快照/屏幕截图的主要内容,如果未能解决你的问题,请参考以下文章