xcode 在 UIWebView 不工作的情况下显示 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9606646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:33:18  来源:igfitidea点击:

Displaying a PDF with UIWebView Not Working

iosxcodeipaduiwebview

提问by Barks

So, I realize that there have been many questions regarding using a UIWebView to display a PDF in an app (on the iPad). I have reviewed everything I can find but I can't seem to find any satisfaction.

所以,我意识到有很多关于使用 UIWebView 在应用程序(在 iPad 上)中显示 PDF 的问题。我已经了我能找到的所有东西,但我似乎找不到任何满意的地方。

What I'm trying to do is very basic so I really don't know why it's not working. All I need to do is display a locally stored PDF in the UIWebView. Essentially, all I'm getting is a black screen. If someone could take a look at my code, I'd really appreciate it. Thanks.

我正在尝试做的是非常基本的,所以我真的不知道为什么它不起作用。我需要做的就是在 UIWebView 中显示本地存储的 PDF。基本上,我得到的只是一个黑屏。如果有人可以看看我的代码,我真的很感激。谢谢。

   - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Segue to the materials screen.

    if ([segue.identifier isEqualToString:@"materials"]) {
        PDFViewController *pdfViewController = [segue destinationViewController];

        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        int row = [path row];
        Lesson *selected = [purchasedLessons objectAtIndex:row];
        pdfViewController.selectedLesson = selected ;

    //Start Load PDF in UIWebView

        pdfViewController.pdfWindowTitle.title = selected.titleAndSubtitle;

        pdfViewController.pdfWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 704)];

        NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"moonlightSonataFirstMovement" ofType:@"pdf"];
        NSURL *url = [NSURL fileURLWithPath:urlAddress];

        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

        [pdfViewController.pdfWebView loadRequest:requestObj];

    //End Load PDF

    }
}

I've checked to see if my object is bring passed into the scene properly (and it is) and if I am getting a proper request out; i'm getting:

我已经检查过我的对象是否被正确地传递到场景中(确实如此),以及我是否收到了正确的请求;我越来越:

<NSURLRequest file://localhost/Users/MYCOMPUTER/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/AB161E57-D942-44C2-AA75-030087820BED/iLessons%20Piano.app/moonlightSonataFirstMovement.pdf>

<NSURLRequest file://localhost/Users/MYCOMPUTER/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/AB161E57-D942-44C2-AA75-030087820BED/iLessons%20Piano.app/moonlightSonataFirstMovement.pdf>

Also, I've have this error message:

另外,我有这个错误信息:

iLessons Piano[23080:f803] DiskImageCache: Could not resolve the absolute path of the old directory.

Additionally, the NSLog message gets printed out 8 times for some reason.

此外,由于某种原因,NSLog 消息被打印了 8 次。

The only thing I can think of is that I need to do this loading when I call my prepareForSegue function in the previous scene. That or use a different method like Quartz. But I'd rather use a UIWebView since all I really need to do is display and allow scrolling.

我唯一能想到的是,我需要在上一个场景中调用我的prepareForSegue函数时进行此加载。或者使用不同的方法,比如 Quartz。但我更愿意使用 UIWebView,因为我真正需要做的就是显示并允许滚动。

采纳答案by Malek_Jundi

im not sure about whats going on .. you view and webView both holding nil value.. as i told you im not involved in the storyBoard yet .. but you as a workaround solution maybe this fix your problem

我不确定发生了什么..你的视图和 webView 都保持 nil 值..正如我告诉你的我还没有参与到 storyBoard 中..但是你作为一个变通的解决方案也许这可以解决你的问题

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1027, 768)];
    pdfViewController.view = view;
    pdfViewController.pdfWebView = [[UIWebView alloc] initWithFrame:pdfViewController.view.frame];

    NSString *urlAddress = [[NSBundle mainBundle] pathForResource:@"moonlightSonataFirstMovement" ofType:@"pdf"];
    NSURL *url = [NSURL fileURLWithPath:urlAddress];

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [pdfViewController.view addSubview:pdfViewController.pdfWebView];
    [pdfViewController.pdfWebView loadRequest:requestObj];

回答by deejfit

DiskImageCache: Could not resolve the absolute path of the old directory.

DiskImageCache:无法解析旧目录的绝对路径。

This one isn't the real reason the app crashes. This warning can be fixed by assigning the view in the Storyboard. It seems like it's connected already but it's grey. So assign it again and it will be fine.

这不是应用程序崩溃的真正原因。可以通过在 Storyboard 中分配视图来修复此警告。它似乎已经连接,但它是灰色的。所以重新分配它就可以了。

The real issue for me was that the PDF images were 300 DPI and it took too long to load the application. Since the debugger keeps the app from crashing it seems to work fine but the loading will take too long without the debugger and will result in a timeout crash.

对我来说真正的问题是 PDF 图像是 300 DPI,加载应用程序需要很长时间。由于调试器可以防止应用程序崩溃,它似乎可以正常工作,但是如果没有调试器,加载将花费太长时间,并会导致超时崩溃。

There's a few things you can do. You can downscale your PDF which might be a good thing anyway since older device are even slower and it's nice to support those as well. But what really fixes it is by delaying the PageView initialization. Here's what I did:

你可以做几件事。您可以缩小您的 PDF 的尺寸,这可能是一件好事,因为旧设备甚至更慢,而且支持这些设备也很好。但真正解决它的是通过延迟 PageView 初始化。这是我所做的:

In the RootViewController I moved the code from the viewDidLoadto a new function setupPageViewer.

在 RootViewController 中,我将代码从 移至viewDidLoad新函数setupPageViewer

And put this in the viewDidLoad:

并将其放入viewDidLoad

[self performSelector:@selector(setupPageViewer) withObject:nil afterDelay:0.0f];

[self performSelector:@selector(setupPageViewer) withObject:nil afterDelay:0.0f];

The delay 0.0 means it will be taken care of in the next frame which gives you the opportunity to show a loading indicator.

延迟 0.0 意味着它将在下一帧中得到处理,这使您有机会显示加载指示器。

Enjoy!

享受!

回答by Apple

As a workaround, you can disable or remove your 'All Exceptions' breakpoint. This might make debugging a little more difficult, but it's not as bad as having to relaunch the application all the time.

作为解决方法,您可以禁用或删除“所有异常”断点。这可能会使调试更加困难,但它并不像必须一直重新启动应用程序那么糟糕。

enter image description here

在此处输入图片说明

This is the breakpoint causing the issue. I had set it so long ago that I'd forgotten it was there

这是导致问题的断点。我很久以前就设置了,以至于我忘记了它在那里