【发布时间】:2012-05-02 06:20:51
【问题描述】:
我尝试将网页导出为图像,以便将其分享到一些不允许输入太多单词的社交网络。当我尝试将 UIWebview 的滚动视图层渲染到当前上下文并创建图像时,只需捕捉页面的可见矩形。我该怎么做?非常感谢!
【问题讨论】:
标签: iphone image uiwebview web
我尝试将网页导出为图像,以便将其分享到一些不允许输入太多单词的社交网络。当我尝试将 UIWebview 的滚动视图层渲染到当前上下文并创建图像时,只需捕捉页面的可见矩形。我该怎么做?非常感谢!
【问题讨论】:
标签: iphone image uiwebview web
NSString *hStr = [web stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];//get the height
NSString *wtStr = [web stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth;"];// Get the width
web.frame=CGRectMake(0, 0, [wtStr floatValue], [hStr floatValue]);
UIGraphicsBeginImageContext(CGSizeMake(web.frame.size.width,web.frame.size.height));
CGContextRef ctx = UIGraphicsGetCurrentContext();
[web.layer renderInContext:ctx];
UIImage *finalimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
尝试根据页面宽度和高度设置webview的高度和宽度,然后截图
【讨论】:
你可以试试这个方法
UIGraphicsBeginImageContext(CGSizeMake(yourwebview.frame.size.width,yourwebview.frame.size.height));
CGContextRef ctx = UIGraphicsGetCurrentContext();
[yourwebview.layer renderInContext:ctx];
finalimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
【讨论】: