将UIWebview内容转换为UIImage [英] Convert UIWebview contents to a UIImage

查看:171
本文介绍了将UIWebview内容转换为UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用UIWebView来显示HTML格式的文本。我没有加载网页,只是提供一个HTML的字符串到UIWebView。

I am using a UIWebView to display HTML formatted text. I am not loading a webpage, just supplying a string of HTML to the UIWebView.

现在我想在屏幕上为这个UIWebView设置动画效果,其实有几个(一次2到10个)。 UIWebView有点重,虽然我还没有尝试,但我计划最糟糕的。 (我不认为这是过早的优化,我几乎肯定这将是一个问题)

Now I want to animate this UIWebView on screen, actually several of them (2-10 at a time). UIWebView is a little heavy, and although I haven't attempted it yet, I am planning for the worst. (I don't think this is premature optimization, I 'm almost positive this will be an issue)

为了解决这个问题,我想我可以转换内容

To get around the problem, I figured I could convert the contents of the UIWebViews to UIImages and animate them instead.

因此,我的问题是:


  1. 如何将UIWebview
    内容转换为UIImage(或
    CGImageRef)?

  2. 我的UIWebView有
    的透明度,
    的透明度是否会转移到
    UIImage?

感谢任何建议

推荐答案

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

如果webview维度很大,可能会遇到问题,因为webview使用

You might run into issues if the webview dimensions are large because the webview uses a CATiledLayer that doesn't draw everything for memory reasons.

图片将包含透明度

这篇关于将UIWebview内容转换为UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆