ios - 如何对具有透明背景的 WKWebView 进行快照?
问题描述
我有WKWebView
一个透明背景,我想在保留透明度的同时捕获图像中的网络内容。我无法使用takeSnapshotWithConfiguration
,drawViewHierarchyInRect
或renderInContext
. 我想这可能是不可能的。
这是我的takeSnapshotWithConfiguration
方法代码:
WKSnapshotConfiguration *wkSnapshotConfig = [WKSnapshotConfiguration new];
wkSnapshotConfig.snapshotWidth = [NSNumber numberWithInt:180];
[_webView takeSnapshotWithConfiguration:wkSnapshotConfig completionHandler:^(UIImage * _Nullable snapshotImage, NSError * _Nullable error) {
NSString *tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"img.png"];
NSData *photoData = UIImagePNGRepresentation(snapshotImage);
[photoData writeToFile:tempFilePath atomically:YES];
}];
解决方案
问题是它_webView
本身具有不透明度。因此,即使显示的内容包含透明度,它们也基本上是在视图的背景上呈现的。
我能够捕获具有透明度的图像,具有像这样的内联样式的最小 html(请原谅我的 html 技能:P):
body {
background: rgba(0, 0, 0, 0);
}
我已经在 iOS 11+ 上验证了这一点,只需将opaque
属性设置为 webview(请注意,我没有为 webview 或其嵌入式滚动视图设置背景颜色。如果您的设置不同,我猜您也应该将它们设置为清除颜色):
对象
_webView.opaque = NO;
迅速
webView.isOpaque = false
其他一切都与您的设置完全相同(WKSnapshotConfiguration
/ takeSnapshot...
)
推荐阅读
- mysql - 修复 VB.NET 错误“连接必须有效且打开”
- ios - 从外部 url 打开已安装的 PWA
- python - Matplotlib figsize 不被尊重
- facebook-ads-api - 我的框架中的 FBAudienceframework 迅速但它给出了最终类异常
- sql-server - SQL Server 用户名和密码
- django - 在 django 中更改 robots.txt
- php - 错误“Yajra \ Pdo \ Oci8 \ Exceptions \ Oci8Exception ORA-24415:用户名丢失或为空。” 使用 Laravel5.6
- spotfire - 直线之间的距离
- c - 形成最小的数
- amazon-web-services - 如何检索 AWS 中正在运行的 Step Functions 的当前状态