Question

Je suis en train de convertir un NSImage à un CIImage. Quand je fais cela, il semble y avoir une énorme perte de qualité d'image. Je pense qu'il est à cause de la « TIFFRepresentation ». Quelqu'un at-il une meilleure méthode? Merci beaucoup.

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];
Était-ce utile?

La solution 3

J'ai finalement résolu le problème. En fait, je rends le document pdf deux fois son offscreen de résolution normale et capture l'image affichée par la vue. Pour une image plus détaillée, il suffit d'augmenter le facteur d'échelle. S'il vous plaît voir le code ci-dessous pour la preuve de concept. Je ne montre pas la CIImage mais une fois que vous obtenez le bitmap, il suffit d'utiliser la méthode de CIImage pour créer le CIImage du 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];

Autres conseils

Votre problème est en effet de convertir au format TIFF. PDF est un format vectoriel, alors que TIFF est bitmap, donc un TIFF regardera floue à des tailles plus grandes.

Votre meilleur pari est probablement pour obtenir un CGImage du NSImage et créer le CIImage de cela. Soit ça ou tout simplement créer le CIImage à partir des données d'origine.

Essayez de remplacer la ligne

NSData  * tiffData = [image TIFFRepresentation];

avec

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

parce que les états de la documentation TIFFRepresentation utilise l'option de compression TIFF associée à chaque représentation d'image, ce qui pourrait ne pas être NSTIFFCompressionNone. Ainsi, vous devez être explicite au sujet de vouloir le tiffData non compressé.

scroll top