将PDFPage添加到PDFDocument时,PDFView不会更新

时间:2014-02-02 23:27:55

标签: objective-c macos cocoa pdfview pdfpage

我正在制作一个扫描多页pdf文件的应用程序。我有一个链接的PDFViewPDFThumbnailView。第一次扫描完成后,我会创建一个新的PDFDocument并将其设置为PDFView。然后,每当完成另一次扫描时,我将PDFPage添加到[pdfView document]

现在问题是每当添加页面时,PDFViewPDFThumbnailView都不会更新以显示带有额外页面的新文档。这是直到我放大或缩小,然后他们都更新以显示带有新页面的文档。

我现在拥有的临时解决方案(放大然后自动缩放)肯定不是最好的解决方案。例如,当您已放大文档并扫描新页面时,视图将自动缩放。我以前试过[pdfView setNeedsDisplay:YES],但似乎没有用。

这是扫描到达NSData的方法:

- (void)scannerDevice:(ICScannerDevice *)scanner didScanToURL:(NSURL *)url data:(NSData *)data {
    //Hide the progress bar
    [progressIndicator stopAnimation:nil];

    //Create a pdf page from the data
    NSImage *image = [[NSImage alloc] initWithData:data];
    PDFPage *page = [[PDFPage alloc] initWithImage:image];

    //If the pdf view has a document
    if ([pdfView document]) {
        //Set the page number and add it to the document
        [page setValue:[NSString stringWithFormat:@"%d", [[pdfView document] pageCount] + 1] forKey:@"label"];
        [[pdfView document] insertPage:page atIndex:[[pdfView document] pageCount]];
    } else {
        //Create a new document and add the page
        [page setValue:@"1" forKey:@"label"];
        PDFDocument *document = [[PDFDocument alloc] init];
        [document insertPage:page atIndex:0];
        [pdfView setDocument:document];
    }

    //Force a redraw for the pdf view so the pages are shown properly
    [pdfView zoomIn:self];
    [pdfView setAutoScales:YES];
}

有没有人知道我可以添加PDFPage并进行PDFView更新的方式而不会弄乱PDFView的缩放状态?

2 个答案:

答案 0 :(得分:3)

您需要手动调用:

- (void)layoutDocumentView

我认为手动调用的原因是允许将多个更改合并为一个更新。

记录在案:

  

PDFView实际上包含多个子视图,例如文档   视图(实际绘制PDF的位置)和“无光泽视图”(可以使用   在PDF内容周围显示为灰色区域,具体取决于   缩放)。对PDF内容的更改可能需要对这些内容进行更改   内部视图,因此如果使用PDF,则必须显式调用此方法   用于添加或删除页面,旋转页面或执行的工具包实用程序类   影响可见布局的其他操作。

     

从影响的PDFView方法自动调用此方法   可见布局(例如setDocument:,setDisplayBox:或zoomIn :)。

答案 1 :(得分:0)

到目前为止,我手动刷新带有注解的PDFView的最佳解决方案是将PDFView移至另一页并返回到需要刷新的页面,因为:

- (void)layoutDocumentView不适用于我。

然后:

[self.pdfView goToLastPage:nil];
[self.pdfView goToPage:destinationPage];
相关问题