Powered By Blogger
Showing posts with label UIWebView. Show all posts
Showing posts with label UIWebView. Show all posts

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

Saturday, September 22, 2012

how to detect link in UIWebview?

Hi friends,
Again I'm here to share something new with you.. If you want to do some operations on URL click in UIWebview of iPhone/iPod/iPad then you need to use the below delegate method of UIWebview. You have to set WebViewDelegate in header file of your class.


#pragma mark-------------------------
#pragma mark WebView Delegate Method


- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = request.URL;
    NSString *urlString = url.relativeString;
    NSLog(@"%@",urlString);
    NSLog(@"%@",[request.URL lastPathComponent]);
    if (navigationType == UIWebViewNavigationTypeLinkClicked)
    {
        if([[urlString substringToIndex:5] isEqualToString:@"https"] || [[urlString substringToIndex:5] isEqualToString:@"http:"])
        {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString: urlString]];
        }
        else
        {
               // do your operation within Application
        }
        return NO;
    }
    return YES;
}

Hope you like this..

Thanks ,
Nilesh Prajapati