Powered By Blogger

Thursday, February 21, 2013

How to capture snapshot of UIWebview in iPhone?


Hi everyone,
Have you ever tried to take a picture of "UIWebView" in any of your applications? Now, you can do it by following below code in your project. You can use scrollview property of UIWebView in iOS 5.0 and later SDK.

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

For example:

#pragma mark --------------------
#pragma mark Take screenshot from Webview
#pragma mark --------------------


UIWebView *receiptWeb;
- (UIImage *)takeScreenshotFromWebview
{
    UIImage *img = nil;
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0"))
    {
        UIGraphicsBeginImageContextWithOptions(receiptWeb.scrollView.contentSize, receiptWeb.scrollView.opaque, 0.0);
        {
            CGPoint savedContentOffset = receiptWeb.scrollView.contentOffset;
            CGRect savedFrame = receiptWeb.scrollView.frame;
          
            receiptWeb.scrollView.contentOffset = CGPointZero;
            receiptWeb.scrollView.frame = CGRectMake(0, 0, receiptWeb.scrollView.contentSize.width, receiptWeb.scrollView.contentSize.height);
            [receiptWeb.scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
            img = UIGraphicsGetImageFromCurrentImageContext();
          
            receiptWeb.scrollView.contentOffset = savedContentOffset;
            receiptWeb.scrollView.frame = savedFrame;
        }
        UIGraphicsEndImageContext();
    }
    else
    {
        for (id subview in receiptWeb.subviews)
        {
            if ([subview isKindOfClass:[UIScrollView class]])
            {
                UIScrollView *scrollView = (UIScrollView *)subview;
                UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, scrollView.opaque, 0.0);
                {
                    CGPoint savedContentOffset = scrollView.contentOffset;
                    CGRect savedFrame = scrollView.frame;
                  
                    scrollView.contentOffset = CGPointZero;
                    scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
                    [scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
                    img = UIGraphicsGetImageFromCurrentImageContext();
                  
                    scrollView.contentOffset = savedContentOffset;
                    scrollView.frame = savedFrame;
                }
                UIGraphicsEndImageContext();
            }
        }
    }
    return img;
}

Regards,
Nilesh Prajapati

2 comments:

  1. can u please check this question once http://stackoverflow.com/questions/20878110/facing-trouble-to-get-the-screen-shot-from-the-uiwebview-when-playing-the-video?noredirect=1#comment31335781_20878110 i tried your code also but same problem occurs.. what is the problem?

    ReplyDelete
    Replies
    1. Hi,
      Tried to capture entire view rather than only UIWebView. I think it should work for you.

      Regards,
      Nilesh Prajapati

      Delete