【问题标题】:Printing and saving a web page Using iPhone使用 iPhone 打印和保存网页
【发布时间】:2013-04-24 05:32:18
【问题描述】:

我希望我的 WebView 保存为任何格式的图像或 pdf,

我尝试使用代码保存网页:

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

但我得到了可见的部分..

我想知道如何获取整个网页图像或苹果人如何进行空中打印以便可以打印整个网页。我不想知道“AirPrint 功能”我想知道如何使用 iPhone 获取网页图像..

因为我对 iOS 开发比较陌生。

谁能帮我保存网页的工作代码?

【问题讨论】:

    标签: iphone ios objective-c ios5 ios4


    【解决方案1】:
    - (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;
        }
    }
    

    【讨论】:

    • 我有 1 个疑问.. wen i 向下滚动到第 2 页,然后它将从第 2 页开始保存.. 只有第 1 页才是问题所在..
    • 你在哪里写这个方法?
    • 点击我正在写这个方法..当我滚动到第二页..它将保存当前页面n下一个所有页面都是正确的..
    • 将其保存在 -(void)webViewDidFinishLoad:(UIWebView *)webView 方法中,然后查看
    • 它在“-(void) webViewDidStartLoad:(UIWebView *)webView”和“-(void) webViewDidFinishLoad:(UIWebView *)webView”中不起作用..
    【解决方案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];    
    }
    

    点击按钮时调用此方法

    【讨论】:

    • 只需在您的主目录中查看 png 将在那里.... iphone Simulator --> iOS 版本 --> YourApplication -->
    • 现在检查编辑的答案并运行并在模拟器或设备中查看您的相册
    • 谢谢它对我的帮助,但我可以知道如何保存这张图片,这意味着这张图片太长了.. 我希望它把下一部分保存为另一个图片.. 怎么做那个?
    • 表示您想要的确切方式?
    【解决方案3】:

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

    once check this one

    【讨论】:

    • 谢谢:这个文件中的代码帮助了我一点点.. 获得 2parts 的预览.. 但是如何保存我没有得到..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 2021-05-06
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    相关资源
    最近更新 更多