Rendering a UIWebView into an ImageContext

前端 未结 6 1013
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 12:33

I am trying to capture the contents of a UIWebView including that which is not visible to the user. i.e. The whole web page even though the user is only looking at the top.<

相关标签:
6条回答
  • 2020-12-14 12:56

    I've released an app (Web2Pic) doing that, and please trust me that UIGraphicsBeginImageContext(webView.frame.size); can do nothing except getting a small image from the visible area in our UIWebView ;-(

    The right way is a bit complex but it just works:

    1.Use JavaScript in our UIWebView to get these float values:

       //Whole page size in HTML coordinate
       document.body.scrollWidth
       document.body.scrollHeight
       //UIWebView visible size in HTML coordinate
       window.innerWidth
       window.innerHeight
    

    2.Now we can 'cut' the whole page into dozens of UIWebView-sized small pieces. Then we can capture every small pieces individually and save them into our Cache. I implemented this by calculating page-offsets and use UIGraphicsBeginImageContext(webView.frame.size); to get a array of images. In addition, you should cache the image array into the file system, or the app will eventually crash!

    3.When we finally got all the small pieces, we can start a full-resolution context: UIGraphicsBeginImageContext(CGSizeMake(document.body.scrollWidth,document.body.scrollHeight));

    4.Render every small images into the big context based on the coordinates. And be careful to the corners, the last image in every line/row may not be a full image.

    5.There is still one step left: saving the big image. Do not save it into the PhotoAlbum, because iOS will automatically cut down the resolution of images in the album. Instead, we can save it into the file system and enable the app's iTunes File Share support, or even write a simple in-app photo manager.

    Hope these can help ;-)

    Yichao Peak Ji

    0 讨论(0)
  • 2020-12-14 12:58

    It looks to me like UIWebView renders on demand (witness the checkerboard as you scroll downwards rapidly on a large page), so there won't be anything in the part of the layer below what you can reach until the user scrolls down there. The layer won't be able to write out what it doesn't have, so I don't think you'll be able to capture the whole area.

    As far as scrolling, there aren't any obvious exposed API methods that I can think of to move it down. The web view seems to host a UIScrollView or something similar, which you could try to access by traversing the view hierarchy and use its scrollRectToVisible:animated:, but that doesn't sound like a good long-term solution.

    0 讨论(0)
  • 2020-12-14 12:58

    Just to answer one of my questions, you can scroll the view by using javascript.

    So you use stringByEvaluatingJavaScriptFromString: with a javascript of window.scrollTo(0,%i);, where %i is where you want to scroll to. You can also use scrollY to retrieve the current scroll value.

    0 讨论(0)
  • 2020-12-14 13:01

    All UIViews have a size limit of 1024x1024, so you will probably have programmatically scroll, capture the page in chunks, and stitch them together somehow.

    0 讨论(0)
  • 2020-12-14 13:08

    Set the height of webview equal to the contentsize of scrollview of webview.

    -(UIImage *)captureImage:(UIWebView *)webvw
    {
        webvw.frame=CGRectMake(webvw.frame.origin.x, webvw.frame.origin.y, webvw.frame.size.width, webvw.scrollView.contentSize.height);
        UIGraphicsBeginImageContextWithOptions(webvw.frame.size, NO, 0.0f);
        [webvw.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
        return screenshot;
    }
    
    0 讨论(0)
  • 2020-12-14 13:14

    Resizing the Webview is the way I solved it. If you want a bigger image with more detail, you just have to zoom in before calling this method. Beware that large dimensions could infulence the performance.

    - (UIImage*) imageFromWebview:(UIWebView*) webview{
    
    //store the original framesize to put it back after the snapshot
    CGRect originalFrame = webview.frame;
    
    //get the width and height of webpage using js (you might need to use another call, this doesn't work always)
    int webViewHeight = [[webview stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] integerValue];
    int webViewWidth = [[webview stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth;"] integerValue];
    
    //set the webview's frames to match the size of the page
    [webview setFrame:CGRectMake(0, 0, webViewWidth, webViewHeight)];
    
    //make the snapshot
    UIGraphicsBeginImageContextWithOptions(webview.frame.size, false, 0.0);
    [webview.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //set the webview's frame to the original size
    [webview setFrame:originalFrame];
    
    //and VOILA :)
    return image;
    

    }

    0 讨论(0)
提交回复
热议问题