将NSIMAGE转换为CIIMAGE而不会降解质量
-
03-10-2019 - |
题
我正在尝试将nsimage转换为ciimage。当我这样做时,图像质量似乎会造成巨大损失。我认为这是因为“ tifferpresentation”。有人有更好的方法吗?非常感谢。
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];
解决方案 3
我终于解决了这个问题。基本上,我将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];
其他提示
您的问题确实是转换为TIFF。 PDF是一种矢量格式,而TIFF是位图,因此TIFF在更大尺寸的情况下看起来模糊。
最好的选择可能是从Nsimage获得CGIMAGE,并从中创建CIIMAGE。要么是从原始数据创建CIIMAGE。
尝试更换行
NSData * tiffData = [image TIFFRepresentation];
和
NSData * tiffData = [image TIFFRepresentationUsingCompression: NSTIFFCompressionNone factor: 0.0f];
因为 文档 指出tifferpresentation使用与每个图像表示相关的TIFF压缩选项,这可能不是NSTIFFCOMPRESSIONNONE。因此,您应该明确地希望将Tiffdata未压缩。
不隶属于 StackOverflow