品質を低下させることなく、NSIMAGEをCIIMAGEに変換します
-
03-10-2019 - |
質問
私はnsimageをciimageに変換しようとしています。私がこれを行うと、画質に大きな損失があるようです。それは「ティフフレップレンション」のためだと思います。誰かがより良い方法を持っていますか?どうもありがとう。
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ドキュメントを通常の解像度オフスクリーンの2倍にレンダリングし、ビューによって表示される画像をキャプチャします。より詳細な画像については、スケーリング係数を増やすだけです。概念実証については、以下のコードをご覧ください。 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];
なぜなら ドキュメント TifFrepresentationは、各画像表現に関連付けられたTIFF圧縮オプションを使用していると述べていますが、これはnStiffcompressionNoneではない可能性があります。したがって、TIFFDATAを非圧縮したいことを明確にする必要があります。
所属していません StackOverflow