不降低质量将NSImage转换为CIImage

3

我正在尝试将NSImage转换为CIImage。但是在此过程中,图像质量似乎有很大的损失。我认为这是因为“TIFFRepresentation”。有没有更好的方法?非常感谢。

NSImage *image = [[NSImage alloc] initWithData:[someSource dataRepresentation]];

NSData  * tiffData = [image TIFFRepresentation];
CIImage *backgroundCIImage = [[CIImage alloc] initWithData:tiffData];

CIContext *ciContext = [[NSGraphicsContext currentContext] CIContext];  
[ciContext drawImage:backgroundCIImage atPoint:CGPointZero fromRect:someRect];

TIFF是一种无损位图容器,因此如果原始图像是位图,则似乎不太可能是罪魁祸首。质量以何种方式降低? - Chuck
NSImage的数据源是PDFPage。当我绘制CIImage时,经过从NSImage转换后,图像中的文本非常模糊。我还尝试仅绘制NSImage,似乎没有任何图像质量损失。因此,我得出结论,这一定是TIFF转换引起的。 - David
3个回答

1

你的问题确实是将文件转换为TIFF格式。PDF是矢量图形格式,而TIFF是位图格式,因此在较大尺寸下,TIFF会显得模糊。

你最好的选择可能是从NSImage获取CGImage,并从中创建CIImage。或者直接从原始数据创建CIImage。


1
CGImages和CIImages也是光栅图像。提问者想要的目标是一个光栅图像,所以他无法在不转换为光栅图像的情况下完成此任务。(话虽如此,出于性能原因,通过CGImage而不是TIFF表示可能是正确的解决方案。) - Peter Hosey

0

尝试替换该行

NSData  * tiffData = [image TIFFRepresentation];

使用

NSData  * tiffData = [image TIFFRepresentationUsingCompression: NSTIFFCompressionNone factor: 0.0f];

因为文档指出TIFFRepresentation使用与每个图像表示关联的TIFF压缩选项,这可能不是NSTIFFCompressionNone。因此,您应该明确希望tiffData未经压缩。


我尝试了那个,结果和之前一样(质量下降)。 - David

0

我终于解决了这个问题。基本上,我将pdf文档在屏幕外呈现两倍于正常分辨率的大小,然后捕获视图显示的图像。如果需要更详细的图像,只需增加缩放因子即可。请参见下面的代码以证明概念。我没有展示CIImage,但是一旦您获得位图,只需使用CIImage方法从位图创建CIImage即可。

    NSImage *pdfImage = [[NSImage alloc] initWithData:[[aPDFView activePage] dataRepresentation]];
    NSSize size = [pdfImage size];
    NSRect imageRect = NSMakeRect(0, 0, size.width, size.height);
    imageRect.size.width *= 2; //Twice the scale factor
    imageRect.size.height *= 2; //Twice the scale factor

    PDFDocument *pageDocument = [[[PDFDocument alloc] init] autorelease];
    [pageDocument insertPage:[aPDFView activePage] atIndex:0];

    PDFView *pageView = [[[PDFView alloc] init] autorelease];
    [pageView setDocument:pageDocument];
    [pageView setAutoScales:YES];

    NSWindow *offscreenWindow = [[NSWindow alloc] initWithContentRect:imageRect 
                                                            styleMask:NSBorderlessWindowMask
                                                              backing:NSBackingStoreRetained
                                                                defer:NO];

    [offscreenWindow setContentView:pageView];
    [offscreenWindow display];
    [[offscreenWindow contentView] display]; // Draw to the backing buffer

    // Create the NSBitmapImageRep
    [[offscreenWindow contentView] lockFocus];

    NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect];

    // Clean up and delete the window, which is no longer needed.
    [[offscreenWindow contentView] unlockFocus];

    [compositeImage TIFFRepresentation]];
    NSData *imageData = [rep representationUsingType: NSJPEGFileType properties: nil];
    [imageData writeToFile:@"/Users/David/Desktop/out.jpg" atomically: YES];

    [offscreenWindow release];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接