Search code examples
iphoneiosuiwebviewuiimage

How to get ENTIRE image from UIWebView including offscreen content


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;
}

Solution

  • 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!

    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
    }