将 NSImage 转换为 CIImage 而不会降低质量

发布于 2024-09-19 18:11:06 字数 475 浏览 2 评论 0原文

我正在尝试将 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];

I am trying to convert an NSImage to a CIImage. When I do this, there seems to be a huge loss in image quality. I think it is because of the "TIFFRepresentation". Does anyone have a better method? Thanks a lot.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

骷髅 2024-09-26 18:11:06

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

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

Your problem is indeed converting to TIFF. PDF is a vector format, while TIFF is bitmap, so a TIFF will look blurry at larger sizes.

Your best bet is probably to get a CGImage from the NSImage and create the CIImage from that. Either that or just create the CIImage from the original data.

恋你朝朝暮暮 2024-09-26 18:11:06

尝试将该行替换

NSData  * tiffData = [image TIFFRepresentation];

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

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

Try replacing the line

NSData  * tiffData = [image TIFFRepresentation];

with

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

because the documentation states that TIFFRepresentation uses the TIFF compression option associated with each image representation, which might not be NSTIFFCompressionNone. Thus, you should be explicit about wanting the tiffData uncompressed.

黯然#的苍凉 2024-09-26 18:11:06

我终于解决了这个问题。基本上,我在屏幕外渲染 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];

I finally solved the problem. Basically, I render the pdf document two times its normal resolution offscreen and then capture the image displayed by the view. For a more detailed image, just increase the scaling factor. Please see the code below for the proof of concept. I didn't show the CIImage but once you get the bitmap, just use the CIImage method to create the CIImage from the bitmap.

    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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文