1

I'm trying to capture the full image of the web page.. But its showing the blank image..

what should i do to get the complete Web Page Image?

I have tried with

CGSize overallSize = [WebPageImage sizeThatFits:CGSizeZero];
UIGraphicsBeginImageContext(WebPageImage.scrollView.contentSize);
WebPageImage.bounds = CGRectMake(0, 0, overallSize.width, overallSize.height);
while (WebPageImage.loading) {
    [NSThread sleepForTimeInterval:0.1];
}
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,(unsigned long)NULL), ^(void) {
    UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
});

Please suggest some solution.. WebPageImage is UIWebView..

Raju
  • 39
  • 6

4 Answers4

4
UIGraphicsBeginImageContextWithOptions(WebPageImage.bounds.size, WebPageImage.opaque, 0.0);
[WebPageImage.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
adali
  • 5,977
  • 2
  • 33
  • 40
  • This answer is complete and perfect +1. – Satheesh Apr 19 '13 at 10:54
  • its giving error at "[printWebPage.layer renderInContext:UIGraphicsGetCurrentContext()];" and error is "ViewController.m:66:6: Receiver type 'CALayer' for instance message is a forward declaration".. – Raju Apr 19 '13 at 11:00
  • not sure, you may need to add QuartzCore.framework && #import – adali Apr 19 '13 at 11:02
  • thanks for your answer but its not giving the whole image.. its showing only the wed page part which is displayed on screen.. – Raju Apr 19 '13 at 11:04
  • yeah, but it's the only method to capture the webview's image, you can figure it out you self by adjust the size of the drawing , & maybe draw several times to draw the whole page :) i only can provide a way to reach that – adali Apr 19 '13 at 11:07
  • you can get the webview's contentSize & move the webview's contentOffset , so you can calculate youself & draw the entire page – adali Apr 19 '13 at 11:08
  • but every time i get the web page its size and offset will change with different URL ya.. – Raju Apr 19 '13 at 11:16
  • or you can try to resize the webview to the contentSize , then draw it – adali Apr 19 '13 at 11:38
  • i mean , you can resize it & draw it , then resize back to the original size – adali Apr 19 '13 at 12:41
1

use this function according to your need. below code will give you snapshot of whole webpage

- (UIImage *) imageFromWebView:(UIWebView *)view
{
//    tempframe to reset view size after image was created
    CGRect tmpFrame     = view.frame;

    // set new Frame
    CGRect aFrame       = view.frame;
    aFrame.size.height  = [view sizeThatFits:[[UIScreen mainScreen] bounds].size].height;
    aFrame.size.width   = [view sizeThatFits:[[UIScreen mainScreen] bounds].size].width;
    view.frame          = aFrame;

//     do image magic
    UIGraphicsBeginImageContext([view sizeThatFits:[[UIScreen mainScreen] bounds].size]);
    CGContextRef resizedContext = UIGraphicsGetCurrentContext();
    [self.thmbWebView.layer renderInContext:resizedContext];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // reset Frame of view to origin
    view.frame = tmpFrame;
    return image;
}
Anurag Soni
  • 1,077
  • 10
  • 14
0

Try this:

[WebPageImage.layer renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
Satheesh
  • 10,998
  • 6
  • 50
  • 93
0

try to capture that image with my bellow method...

-(IBAction)captureScreen:(id)sender
{   
    UIGraphicsBeginImageContext(webview.frame.size);
    [webview.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
}

Also see my another answer from this link.. howe-to-capture-uiview-top-uiview

Community
  • 1
  • 1
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • here you get which you want dude... here its working with whole view.. here instead of WebPageImage you can set self.view .. – Paras Joshi Apr 19 '13 at 11:14
  • here set need to set opaque to the UIGraphicsBeginImageContextWithOptions method.. and it will worked like adali says... – Paras Joshi Apr 19 '13 at 11:26
  • its not working here dude.. i tried with other options also.. all are giving the part of web page which is visible.. – Raju Apr 19 '13 at 11:30
  • @Raju here you set option with its above method which have this syntax UIGraphicsBeginImageContextWithOptions(<#CGSize size#>, <#BOOL opaque#>, <#CGFloat scale#>) , so here opaqe pass differ and it will worked... if you used self.view then it worked but here for webview use option... – Paras Joshi Apr 19 '13 at 11:30
  • oh you want to get inner page of UIWebView's screenshot?? – Paras Joshi Apr 19 '13 at 11:45
  • @Raju see now i capture two images and its give me the screenshot of webPage only.. try to change the frame of UIWebView its give you image of that uiwebview's frame size.. try now... :) – Paras Joshi Apr 19 '13 at 11:55
  • still its giving the same half web page image..:( is it possible to get 2 or 3 images, so that we can get entire page? – Raju Apr 19 '13 at 11:58
  • see this link.. http://stackoverflow.com/questions/5763986/iphone-auto-resizing-uiwebview-content-do-not-fit-the-frame hope this help ypu.. :) – Paras Joshi Apr 19 '13 at 12:04
  • No still i'm getting the same partial image of UIWebView..:( – Raju Apr 19 '13 at 12:18