0

I want my WebView to save as image or pdf of any formate,

I tried with saving the web page using the code :

   UIGraphicsBeginImageContext(WebPage.frame.size);
   [WebPage.layer renderInContext:UIGraphicsGetCurrentContext()];
   UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

but i'm getting the part which is visible..

i want to know how to get the entire web page image or how the apple people giving the air print so that the entire web page can b printed.. i dont want to know the "AirPrint function" i want to know how to get web page image using the iPhone..

As i'm fresher to iOS development.

can any one help me with he working code of saving web page?

4

3 回答 3

2
- (void)printAndSave
{
    webViewHeight = [[self.myWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] integerValue];
    CGRect screenRect = self.myWebView.frame;
    double currentWebViewHeight = webViewHeight;
    while (currentWebViewHeight > 0)
    {
        imageName ++;
        UIGraphicsBeginImageContext(screenRect.size);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [[UIColor blackColor] set];
        CGContextFillRect(ctx, screenRect);
        [self.myWebView.layer renderInContext:ctx];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",imageName]];
        if(currentWebViewHeight < 460)
        {
            CGRect lastImageRect = CGRectMake(0, 457 - currentWebViewHeight, self.myWebView.frame.size.width, currentWebViewHeight);
            CGImageRef lastImageRef = CGImageCreateWithImageInRect([newImage CGImage], lastImageRect);
            newImage = [UIImage imageWithCGImage:lastImageRef];
            CGImageRelease(lastImageRef);
        }
        [UIImagePNGRepresentation(newImage) writeToFile:pngPath atomically:YES];
        [self.myWebView stringByEvaluatingJavaScriptFromString:@"window.scrollBy(0,460);"];
        currentWebViewHeight -= 460;
    }
}
于 2013-04-24T06:39:04.947 回答
2
- (IBAction)printSaveTheWebView:(id)sender
{
    UIImage *viewImage;
    UIScrollView *Scroll_view = webView.scrollView;
    CGRect savedFrame;
    UIGraphicsBeginImageContext(Scroll_view.contentSize);
    {
        CGPoint savedContentOffset = Scroll_view.contentOffset;
        savedFrame = Scroll_view.frame;
        Scroll_view.contentOffset = CGPointZero;
        Scroll_view.frame = CGRectMake(0, 0, Scroll_view.contentSize.width, Scroll_view.contentSize.height);
        [Scroll_view.layer renderInContext: UIGraphicsGetCurrentContext()];
        viewImage = UIGraphicsGetImageFromCurrentImageContext();
        Scroll_view.contentOffset = savedContentOffset;
        Scroll_view.frame = savedFrame;
    }
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, self, nil, nil);
    [UIImagePNGRepresentation(viewImage) writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"view.png"] atomically:YES];    
}

单击按钮时调用此方法

于 2013-04-24T05:40:35.247 回答
1

为了获得您的要求,您必须通过更改框架来更改contextof webview,为此需要一个 for 循环并通过更改上下文根据页数循环它。

一旦检查这个

于 2013-04-24T05:36:24.600 回答