gpt4 book ai didi

ios - 如何截屏透明背景的WKWebView?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:17 25 4
gpt4 key购买 nike

我有一个具有透明背景的 WKWebView,我想在保持透明度的同时捕获图像中的 Web 内容。我无法使用 takeSnapshotWithConfigurationdrawViewHierarchyInRectrenderInContext 进行此操作。我认为这可能是不可能的。

这是我的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 本身 不透明。因此,即使显示的内容包含透明度,它们也基本上呈现在 View 的背景上。

我能够捕捉到 image with transparency , 一个像这样的内联样式的最小 html(请原谅我的 html 技能 :P):

body {
background: rgba(0, 0, 0, 0);
}

我已经在 iOS 11+ 上验证了这一点,只需将 opaque 属性设置为 webview(请注意,我没有为 webview 或其嵌入式 ScrollView 设置背景颜色。如果您的设置是不同的我想你也应该将它们设置为清晰的颜色):


ObjC

_webView.opaque = NO;

swift

webView.isOpaque = false

其他一切都与您的设置完全一样(WKSnapshotConfiguration/takeSnapshot...)

关于ios - 如何截屏透明背景的WKWebView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56657701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com