我想以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 中显示的要小? - 我的图像周围有一些边距