1

I want to do a screenshot of a UIWebView, I use this code

Code:

UIGraphicsBeginImageContext(self.vistaWeb.bounds.size);
[vistaWeb.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage,nil,NULL,NULL);

and it work well, my problem is that I want to make a screenshot of all the uiwebview, not only the visible view...for example, I have a uiwebview of 200x200 but the page is 200x500, whit this code I make a screenshot of 200x200 while I want to do a screenshot of 200x500.what I have to do?

kikko088

francesco.venica
  • 1,703
  • 2
  • 19
  • 54

1 Answers1

3

The problem is in the line:

UIGraphicsBeginImageContext(self.vistaWeb.bounds.size);

You are using the size of your view and not the size of its content. In order to obtain the size of your page use the following approach. Then just replace self.vistaWeb.bounds.size with an appropriate size.

In order to support retina you should replace the first line of your code with the following:

CGSize layerSize = ...
if ([UIScreen instancesRespondToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0f) {
    UIGraphicsBeginImageContextWithOptions(layerSize, NO, 2.0f);
} else {
    UIGraphicsBeginImageContext(layerSize);
}
Community
  • 1
  • 1
Anton
  • 2,342
  • 15
  • 17
  • thank you very much!now is perfect....only another question, how can I improve the image quality?now is low-low quality.. – francesco.venica Jun 12 '11 at 20:41
  • @kikko088 the image will be exactly as on the screen. The only problem I can think of is that your code is not adapted to retina devices. Here is the proper way to begin an image context for all devices: http://codinggeek.org/2011/02/21/retina-and-ios-sdk-part-ii-uigraphicsbeginimagecontext/ – Anton Jun 12 '11 at 20:45
  • Here I put 3 screenshot, and photo of the camera roll, the webview is ready to retina display, all image are in svg (then are vectorial image) www.endurodoc.net/1.png www.endurodoc.net/2.png www.endurodoc.net/3.png – francesco.venica Jun 12 '11 at 21:03
  • I try it but the quality is the same...I solved in another way, I create a pdf instead an image... :) – francesco.venica Jun 15 '11 at 18:14