当 webview 大于屏幕时,将 UIWebview 内容转换为 UIImage

发布于 2024-10-08 16:03:10 字数 576 浏览 2 评论 0原文

这个问题非常相似(还有这个答案),我正在尝试从网络视图中制作 UIImage 。到目前为止,我正在使用答案中建议的代码,具体来说:

UIGraphicsBeginImageContext(webview.bounds.size);
[webview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但是,当 webview 的内容大于屏幕时,生成的图像不完整并且缺少补丁。关于如何解决这个问题有什么想法吗?

Very similar to this question (and also this answer), I'm trying to make an UIImage out from a webview. So far I'm using the code suggested in the answer, specifically:

UIGraphicsBeginImageContext(webview.bounds.size);
[webview.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

However, when the contents of the webview are larger than the screen, the resulting images are not complete and have missing patches. Any ideas on how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泼猴你往哪里跑 2024-10-15 16:03:10

我得到了答案:

您必须在创建图像之前将 Webview 的框架设置为完整内容大小。之后,只需将尺寸设置回原点:

+ (UIImage *) imageFromWebView:(UIWebView *)view
{
    // tempframe to reset view size after image was created
    CGRect tmpFrame         = view.frame;

    // set new Frame
    CGRect aFrame               = view.frame;
    aFrame.size.height  = [view sizeThatFits:[[UIScreen mainScreen] bounds].size].height;
    view.frame              = aFrame;

    // do image magic
    UIGraphicsBeginImageContext([view sizeThatFits:[[UIScreen mainScreen] bounds].size]);

    CGContextRef resizedContext = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:resizedContext];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    // reset Frame of view to origin
    view.frame = tmpFrame;
    return image;
}

I got the answer:

You have to set the frame of the Webview to the full content size just before you create the image. After this just set the size back to origin:

+ (UIImage *) imageFromWebView:(UIWebView *)view
{
    // tempframe to reset view size after image was created
    CGRect tmpFrame         = view.frame;

    // set new Frame
    CGRect aFrame               = view.frame;
    aFrame.size.height  = [view sizeThatFits:[[UIScreen mainScreen] bounds].size].height;
    view.frame              = aFrame;

    // do image magic
    UIGraphicsBeginImageContext([view sizeThatFits:[[UIScreen mainScreen] bounds].size]);

    CGContextRef resizedContext = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:resizedContext];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    // reset Frame of view to origin
    view.frame = tmpFrame;
    return image;
}
失退 2024-10-15 16:03:10

DR的回答很好。唯一缺少的是考虑屏幕的比例。我在我的 Swift 版本中更正了它,它对我来说工作得很好。

extension UIWebView{
func snapshotForCompletePage() -> UIImage {
    // tempframe to reset view size after image was created
    let tmpFrame: CGRect = self.frame
    // set full size Frame
    var fullSizeFrame: CGRect = self.frame
    fullSizeFrame.size.height = self.scrollView.contentSize.height
    self.frame = fullSizeFrame

    // here the image magic begins
    UIGraphicsBeginImageContextWithOptions(fullSizeFrame.size, false, UIScreen.mainScreen().scale)
    let resizedContext: CGContextRef = UIGraphicsGetCurrentContext()!
    self.layer.renderInContext(resizedContext)
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    // reset Frame of view to origin
    self.frame = tmpFrame
    return image
      }
    }

D R's answer is fine. The only thing missing is taking into account the scale of screen. I have it corrected in my Swift version and it works fine for me.

extension UIWebView{
func snapshotForCompletePage() -> UIImage {
    // tempframe to reset view size after image was created
    let tmpFrame: CGRect = self.frame
    // set full size Frame
    var fullSizeFrame: CGRect = self.frame
    fullSizeFrame.size.height = self.scrollView.contentSize.height
    self.frame = fullSizeFrame

    // here the image magic begins
    UIGraphicsBeginImageContextWithOptions(fullSizeFrame.size, false, UIScreen.mainScreen().scale)
    let resizedContext: CGContextRef = UIGraphicsGetCurrentContext()!
    self.layer.renderInContext(resizedContext)
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    // reset Frame of view to origin
    self.frame = tmpFrame
    return image
      }
    }
忆梦 2024-10-15 16:03:10

@Jibeex 的答案对我有用。但我必须

    fullSizeFrame.size.width = self.scrollView.contentSize.width

在下面

    fullSizeFrame.size.height = self.scrollView.contentSize.height

添加以下代码才能使其完全正常工作。写为答案是因为我没有足够的声誉来发表评论。

@Jibeex's answer worked for me. But I had to add the following code

    fullSizeFrame.size.width = self.scrollView.contentSize.width

below

    fullSizeFrame.size.height = self.scrollView.contentSize.height

for it to fully work. Writing as answer because I don't have enough reputation to comment.

他夏了夏天 2024-10-15 16:03:10

我认为这会有所帮助

UIGraphicsBeginImageContext([webView sizeThatFits:[[UIScreen mainScreen] bounds].size]]);

I think this could help

UIGraphicsBeginImageContext([webView sizeThatFits:[[UIScreen mainScreen] bounds].size]]);
夜血缘 2024-10-15 16:03:10

如果有人正在努力如何在 Xamarin 中执行此操作,根据 @DR 答案,以下是代码:

var image = new UIImage();

var previousFrame = _tableView.Frame;
var newFrame = new CGRect(new CGPoint(0, 0), _tableView.ContentSize);
View.Frame = newFrame;

UIGraphics.BeginImageContextWithOptions(_tableView.ContentSize, false, UIScreen.MainScreen.Scale);
var context = UIGraphics.GetCurrentContext();
View.Layer.RenderInContext(context);
image = UIGraphics.GetImageFromCurrentImageContext();

UIGraphics.EndImageContext();

View.Frame = previousFrame;


return image;

If someone is struggling how to do this in Xamarin, based on @DR answer, here is the code:

var image = new UIImage();

var previousFrame = _tableView.Frame;
var newFrame = new CGRect(new CGPoint(0, 0), _tableView.ContentSize);
View.Frame = newFrame;

UIGraphics.BeginImageContextWithOptions(_tableView.ContentSize, false, UIScreen.MainScreen.Scale);
var context = UIGraphics.GetCurrentContext();
View.Layer.RenderInContext(context);
image = UIGraphics.GetImageFromCurrentImageContext();

UIGraphics.EndImageContext();

View.Frame = previousFrame;


return image;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文