我尝试将网页导出到图片,以便将其分享到某个不允许输入太多单词的社交网络。当我尝试将UIWebview的scrollview层渲染到当前上下文并创建图像时,只需捕捉页面的可见rect。我该怎么做?非常感谢你!
答案 0 :(得分:1)
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();
尝试根据页面宽度和高度设置webviews的高度和宽度,然后截取屏幕截图
答案 1 :(得分:0)
你可以这样试试
UIGraphicsBeginImageContext(CGSizeMake(yourwebview.frame.size.width,yourwebview.frame.size.height));
CGContextRef ctx = UIGraphicsGetCurrentContext();
[yourwebview.layer renderInContext:ctx];
finalimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();