代码之家  ›  专栏  ›  技术社区  ›  David

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

  •  2
  • David  · 技术社区  · 15 年前

    我正在尝试将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 回复  |  直到 15 年前
        1
  •  1
  •   Chuck    15 年前

    你的问题确实是转化成了TIFF。PDF是矢量格式,而TIFF是位图格式,因此TIFF在较大的尺寸下看起来会很模糊。

        2
  •  0
  •   AlcubierreDrive    15 年前

    试着更换线路

    NSData  * tiffData = [image TIFFRepresentation];
    

    具有

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

    the documentation 说明TIFF表示使用与每个图像表示关联的TIFF压缩选项,该选项可能不是NSTIFFCompressionNone。因此,您应该明确表示希望数据未压缩。

        3
  •  0
  •   David    15 年前

        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];
    
    推荐文章