开发者

Convert UIWebview contents to a UIImage when the webview is larger than the screen

Very similar to this question (and also this answer), I'm trying to make an UIImage out from a webview. So far I'm using the code suggested in the answer, specifically:

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

Howev开发者_JS百科er, when the contents of the webview are larger than the screen, the resulting images are not complete and have missing patches. Any ideas on how to fix this?


I got the answer:

You have to set the frame of the Webview to the full content size just before you create the image. After this just set the size back to origin:

+ (UIImage *) imageFromWebView:(UIWebView *)view
{
    // tempframe to reset view size after image was created
    CGRect tmpFrame         = view.frame;

    // set new Frame
    CGRect aFrame               = view.frame;
    aFrame.size.height  = [view sizeThatFits:[[UIScreen mainScreen] bounds].size].height;
    view.frame              = aFrame;

    // do image magic
    UIGraphicsBeginImageContext([view sizeThatFits:[[UIScreen mainScreen] bounds].size]);

    CGContextRef resizedContext = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:resizedContext];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    // reset Frame of view to origin
    view.frame = tmpFrame;
    return image;
}


D R's answer is fine. The only thing missing is taking into account the scale of screen. I have it corrected in my Swift version and it works fine for me.

extension UIWebView{
func snapshotForCompletePage() -> UIImage {
    // tempframe to reset view size after image was created
    let tmpFrame: CGRect = self.frame
    // set full size Frame
    var fullSizeFrame: CGRect = self.frame
    fullSizeFrame.size.height = self.scrollView.contentSize.height
    self.frame = fullSizeFrame

    // here the image magic begins
    UIGraphicsBeginImageContextWithOptions(fullSizeFrame.size, false, UIScreen.mainScreen().scale)
    let resizedContext: CGContextRef = UIGraphicsGetCurrentContext()!
    self.layer.renderInContext(resizedContext)
    let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    // reset Frame of view to origin
    self.frame = tmpFrame
    return image
      }
    }


@Jibeex's answer worked for me. But I had to add the following code

    fullSizeFrame.size.width = self.scrollView.contentSize.width

below

    fullSizeFrame.size.height = self.scrollView.contentSize.height

for it to fully work. Writing as answer because I don't have enough reputation to comment.


I think this could help

UIGraphicsBeginImageContext([webView sizeThatFits:[[UIScreen mainScreen] bounds].size]]);


If someone is struggling how to do this in Xamarin, based on @DR answer, here is the code:

var image = new UIImage();

var previousFrame = _tableView.Frame;
var newFrame = new CGRect(new CGPoint(0, 0), _tableView.ContentSize);
View.Frame = newFrame;

UIGraphics.BeginImageContextWithOptions(_tableView.ContentSize, false, UIScreen.MainScreen.Scale);
var context = UIGraphics.GetCurrentContext();
View.Layer.RenderInContext(context);
image = UIGraphics.GetImageFromCurrentImageContext();

UIGraphics.EndImageContext();

View.Frame = previousFrame;


return image;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜