【问题标题】:Extract NSImage from PDFPage with varying resolution以不同的分辨率从 PDFPage 中提取 NSImage
【发布时间】:2013-03-25 21:29:30
【问题描述】:

从PDFPage 对象获取NSImage 的最佳方法是什么?

我有一个显示在PDFView 中的PDF 文件,我需要捕获当前页面并存储为NSImage。此外,我需要能够改变页面的分辨率 (DPI)。

使用此代码,A4 页面的结果图像仅为 595x841。我需要比这更高的分辨率(最高 300DPI):

PDFPage *page = _pdfView.currentPage;
NSData *pageData = page.dataRepresentation;
NSImage *img = [[NSImage alloc] initWithData:pageData];

【问题讨论】:

    标签: cocoa pdf nsimage


    【解决方案1】:

    我终于设法解决了这个问题:

    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];
    

    【讨论】:

      猜你喜欢
      • 2011-03-26
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 2016-03-15
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多