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