5

如何让 PDFView 显示 PDF 而无需调整窗口大小或滚动以强制重绘。

好像setNeedsDisplay需要做一个,但没有效果。

我有我能想到的最简单的应用程序,可以使用 PDFView 显示 PDF。

基本上在 Xcode 中创建一个新应用程序并将代码更改为:

// AppDelegate.h
#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet PDFView *pdfView;
@end

// AppDelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize pdfView = _pdfView;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:@"TestPage" withExtension:@"pdf"];
    PDFDocument *document = [[PDFDocument alloc] initWithURL: pdfURL];     
    self.pdfView.document = document;
}
@end

我在 Apple 代码示例PDFKitLinker2中注意到了相同的行为,其中必须滚动 PDF 或调整窗口大小才能显示 PDF。

诚然,我是 Cocoa 开发领域的新手,但我已经搜索并没有找到显示如何显示 PDF 的非常小的示例。我制作的内容似乎是正确的,但我怀疑即使PDFKitLinker2似乎也不正确。

我创建了一个Xcode 项目的 Git 存储库,为那些有时间提供帮助和/或评论的人演示了这个问题。

git clone git@bitbucket.org:sotapme/pdfview_open.git

使用 Xcode 版本 4.3.3 (4E3002) - 最新的 Lion。

提前感谢您的帮助。


一个有效的解决方案。


我找到了一个解决方案,如果我创建一个具有空实现的 PDFView 子类,然后将其添加为自定义视图并将其连接到我现有的IBOutlet它可以工作。

对我来说完全没有意义:(

//DPPDFView.h
#import <Quartz/Quartz.h>
@interface DPPDFView : PDFView
@end

//DPPDFView.m
#import "DPPDFView.h"
@implementation DPPDFView
@end

苹果的文档确实说:

PDFView 可能是您向应用程序添加 PDF 功能时唯一需要处理的类。它允许您显示 PDF 数据并允许用户选择内容、浏览文档、设置缩放级别以及将文本内容复制到粘贴板。PDFView 还跟踪页面历史记录。

您可以将 PDFView 子类化以创建自定义 PDF 查看器。

我把这个问题和自己的答案留给那些可能会遇到类似问题的人,希望这对他们来说是一盏昏暗的灯塔;也希望有更多可可智慧的人指出我的想法错误。

4

3 回答 3

2

我认为这确实是由于延迟加载完成实际加载 PDF 文档后PDFview 需要 setNeedsDisplay 引起的。

一个 hack,但是将以下内容添加到您的 - (void)windowDidLoad 方法中至少可以正确显示文档而无需调整大小。

dispatch_async(dispatch_get_main_queue(), ^{
    [self.myPDFView setNeedsDisplay:YES];
});
于 2012-10-26T15:49:29.713 回答
0

这是一个假设,但可能是因为当您self.pdfView.document = document;未实例化 NIB 中的 PDFView 时!

nil当您向对象传递消息时,Obj-C 不会对您大喊大叫。

为了检验这个假设,你可以NSLog()看看pdfView它是否是nil

于 2013-04-04T16:05:34.813 回答
0

我发现在视图中设置“Wants Core Animation”标志会导致一些奇怪的行为。如果您有该设置,请尝试添加:

[pdfView setLayerContentsRedrawPolicy: NSViewLayerContentsRedrawOnSetNeedsDisplay];
[pdfView setNeedsDisplay: YES];

到加载 PDFView 的函数。

于 2012-06-29T20:25:56.720 回答