How to get ENTIRE image from UIWebView including offscreen content(如何从 UIWebView 获取整个图像,包括屏幕外内容)
问题描述
我有一种方法可以从我的 iOS 应用程序中的各种视图中获取图像,以便让用户通过电子邮件发送屏幕.我绘制的大多数屏幕都可以正常工作,但是当我将这种技术与 UIWebView 一起使用时,我只能得到屏幕的可见部分.屏幕外的任何内容都不包含在渲染图像中.一直在 Stack 上挖掘,但到目前为止我还没有找到工作?!
I have a method I use to get images from a various views in my iOS application for letting users email screens. Most of the screens where I draw are working ok, but when I use this technique with a UIWebView I only get the visible portion of the Screen. Anything off screen is not included in the rendered image. Been digging around here on Stack, but so far nothing I have found works?!
这是我目前使用的方法:
Here is the method I currently use:
-(NSData *)getImageFromView:(UIView *)view
{
NSData *pngImg;
CGFloat max, scale = 1.0;
CGSize size = [view bounds].size;
// Scale down larger images else we run into mem issues with mail widget
max = (size.width > size.height) ? size.width : size.height;
if( max > 960 )
scale = 960/max;
UIGraphicsBeginImageContextWithOptions( size, YES, scale );
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
pngImg = UIImagePNGRepresentation( UIGraphicsGetImageFromCurrentImageContext() );
UIGraphicsEndImageContext();
return pngImg;
}
推荐答案
哇,答案简直太简单了……正在到处寻找各种与打印/PDF相关的东西……然后我突然想到,为什么不只是将上下文中的视图设置为 sizeThatFits.成功了!
Wow the answer was stupidly simple... was digging all over the place looking at various Printing/PDF related stuff... then it occurred to me, why not just set the view IN THE CONTEXT to a sizeThatFits. It worked!
警告:不能保证您不会遇到内存问题,我建议您在 @autoreleasepool 池中执行此操作,并考虑像我在示例中所做的那样进行一些缩放,但这有效并且是我决定的:
WARNING: No guarantee you don't run into mem issues with this and I DO suggest you do this inside an @autoreleasepool pool and consider doing some scaling as I do in the example, but THIS WORKS and is what I settled on:
-(NSData *)getImageFromView:(UIView *)view // Mine is UIWebView but should work for any
{
NSData *pngImg;
CGFloat max, scale = 1.0;
CGSize viewSize = [view bounds].size;
// Get the size of the the FULL Content, not just the bit that is visible
CGSize size = [view sizeThatFits:CGSizeZero];
// Scale down if on iPad to something more reasonable
max = (viewSize.width > viewSize.height) ? viewSize.width : viewSize.height;
if( max > 960 )
scale = 960/max;
UIGraphicsBeginImageContextWithOptions( size, YES, scale );
// Set the view to the FULL size of the content.
[view setFrame: CGRectMake(0, 0, size.width, size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
pngImg = UIImagePNGRepresentation( UIGraphicsGetImageFromCurrentImageContext() );
UIGraphicsEndImageContext();
return pngImg; // Voila an image of the ENTIRE CONTENT, not just visible bit
}
这篇关于如何从 UIWebView 获取整个图像,包括屏幕外内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 UIWebView 获取整个图像,包括屏幕外内容
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01