How to get ENTIRE image from UIWebView including offscreen content

后端 未结 4 1992
醉酒成梦
醉酒成梦 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:32

    I created a category for this a while ago. It works if (and only if) the web view is cached into memory, otherwise parts of the page will appear as white.

    #import "UIWebView+Screenshot.h"
    #import 
    
    @implementation UIWebView (Screenshot)
    
    - (UIImage *)screenshot {
    UIImage *img = nil;
    
        UIGraphicsBeginImageContextWithOptions(self.scrollView.contentSize, self.scrollView.opaque, 0.0);
        {
            CGPoint savedContentOffset = self.scrollView.contentOffset;
            CGRect savedFrame = self.scrollView.frame;
    
            self.scrollView.contentOffset = CGPointZero;
            self.scrollView.frame = CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height);
            [self.scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
            img = UIGraphicsGetImageFromCurrentImageContext();
    
            self.scrollView.contentOffset = savedContentOffset;
            self.scrollView.frame = savedFrame;
        }
        UIGraphicsEndImageContext();
    
        return img;
    }
    
    @end
    

提交回复
热议问题