1

我想以NSView单页 PDF 显示。

到目前为止,我有两种解决方案,但它们都有缺点。任何人都可以帮助我解决这些缺点吗?

第一个解决方案:使用 NSImage 和 NSImageView

   NSString *path= [[NSBundle mainBundle] pathForResource:name ofType:@"pdf"];
   NSImage * image = [[NSImage alloc] initWithContentsOfFile:path] ;

   NSImageView * imageView = [[NSImageView alloc] init] ;
   imageView.frame = NSMakeRect(0, 0, 2*image.size.width, 2*image.size.height) ;
   imageView.image = image ;
   imageView.imageScaling = NSImageScaleAxesIndependently ;

   return imageView

缺点

  • 图像没有抗锯齿
  • 我不明白为什么2需要这个因素。为什么我的 PDF 在 NSView 中显示的比在 Finder 中显示的要小?

第二种解决方案:使用 PDFDocument 和 PDFView

    NSString *path= [[NSBundle mainBundle] pathForResource:name ofType:@"pdf"];
    NSURL *urlPDF = [NSURL fileURLWithPath:path] ;

    PDFDocument * myPDFDocument = [[PDFDocument alloc] initWithURL:urlPDF] ;

    PDFView *myPDFView = [[PDFView alloc] init] ;
    myPDFView.document = myPDFDocument ;

    PDFPage * firstPage = [myPDFDocument pageAtIndex:0] ;
    NSRect myBounds = [firstPage boundsForBox:kPDFDisplayBoxMediaBox] ;
    NSRect myNewBounds = NSMakeRect(0, 0, myBounds.size.width*2, myBounds.size.height*2+5) ;

    myPDFView.frame = myNewBounds ;
    myPDFView.autoScales = YES ;

    return myPDFView ;

缺点

  • 我可以选择我的 pdf 的文本,我可以放大或缩小。但我希望我的 PDF 文档显示为图像,没有这些可能性
  • 我不明白为什么2需要这个因素。为什么我的 PDF 在 NSView 中显示的比在 Finder 中显示的要小?
  • 我的图像周围有一些边距
4

1 回答 1

0

我没有看到你用 NSImageView 描述的问题。我实现了一个基于 nib 的窗口和 NSImageView。就我而言,我有一个重叠的兄弟视图,所以我在笔尖中打开了 CALayers。我在 10.9.2。大小是正常的(1x),我的 PDF 中的文本是抗锯齿的(我认为是亚像素,因为我在放大它时会看到颜色)。我确实有缩放 NONE - 也许缩放会阻止抗锯齿文本?

否则,我的猜测是您的视图或 PDF 内容有所不同。尝试更简单的 PDF 和/或基于 nib 的视图,如果可行,您可以寻找差异。

于 2014-05-05T05:26:05.627 回答