How to get ENTIRE image from UIWebView including offscreen content

后端 未结 4 2020
醉酒成梦
醉酒成梦 2021-01-06 16:55

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 u

4条回答
  •  佛祖请我去吃肉
    2021-01-06 17:28

    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
    }
    

提交回复
热议问题