简体   繁体   English

将UIWebview内容转换为UIImage

[英]Convert UIWebview contents to a UIImage

I am using a UIWebView to display HTML formatted text. 我正在使用UIWebView来显示HTML格式的文本。 I am not loading a webpage, just supplying a string of HTML to the UIWebView. 我没有加载网页,只是向UIWebView提供了一串HTML。

Now I want to animate this UIWebView on screen, actually several of them (2-10 at a time). 现在我想在屏幕上设置这个UIWebView的动画,实际上有几个(一次2-10个)。 UIWebView is a little heavy, and although I haven't attempted it yet, I am planning for the worst. UIWebView有点沉重,虽然我还没有尝试过,但我计划做最坏的事情。 (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. 为了解决这个问题,我想我可以将UIWebViews的内容转换为UIImages,并为它们设置动画。

So, my questions are: 所以,我的问题是:

  1. How do you convert UIWebview contents to a UIImage (or CGImageRef)? 如何将UIWebview内容转换为UIImage(或CGImageRef)?
  2. My UIWebViews have transparent bacgrounds, will the transparency be carried over to the UIImage? 我的UIWebViews有透明的bacgrounds,透明度会转移到UIImage吗?

Thanks for any suggestions 谢谢你的任何建议

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

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. 如果webview维度很大,则可能会遇到问题,因为webview使用的CATiledLayer不会因内存原因而绘制所有内容。

The image will include transparency 图像将包括透明度

I could be wrong but if you set the cache param of CoreAnimation the OS will handle this optimisation for you. 我可能错了,但如果你设置CoreAnimation的缓存参数,操作系统将为你处理这个优化。

eg 例如

[UIView setAnimationTransition:transition forView:self.view cache:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM