从 PDFPage
对象获取具有可变分辨率的 NSImage
的最佳方法是什么?
我有一个 PDF 文件显示在 PDFView
中,我需要捕获当前页面并存储为 NSImage
。此外,我需要能够改变页面的分辨率 (DPI)。
使用此代码,A4 页面的结果图像仅为 595x841。我需要比这更高的分辨率(高达 300DPI):
PDFPage *page = _pdfView.currentPage;
NSData *pageData = page.dataRepresentation;
NSImage *img = [[NSImage alloc] initWithData:pageData];
最佳答案
我终于成功解决了这个问题:
CGFloat factor = 300/72; // Scale from 72 DPI to 300 DPI
NSImage *img; // Source image
NSSize newSize = NSMakeSize(img.size.width*factor, img.size.height*factor);
NSImage *scaledImg = [[NSImage alloc] initWithSize:newSize];
[scaledImg lockFocus];
[[NSColor whiteColor] set];
[NSBezierPath fillRect:NSMakeRect(0, 0, newSize.width, newSize.height)];
NSAffineTransform *transform = [NSAffineTransform transform];
[transform scaleBy:factor];
[transform concat];
[img drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[scaledImg unlockFocus];
关于cocoa - 从不同分辨率的 PDFPage 中提取 NSImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15246563/