سؤال

أحاول تحويل nsimage إلى ciimage. عندما أفعل هذا ، يبدو أن هناك خسارة كبيرة في جودة الصورة. أعتقد أنه بسبب "تمثيل tiffrepresent". هل لدى أي شخص طريقة أفضل؟ شكرًا جزيلاً.

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 ستبدو ضبابية بأحجام أكبر.

أفضل رهان لك هو الحصول على cgimage من nsimage وإنشاء ciimage من ذلك. إما ذلك أو مجرد إنشاء ciimage من البيانات الأصلية.

حاول استبدال الخط

NSData  * tiffData = [image TIFFRepresentation];

مع

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

لان وثائق ينص على أن tiffrepresentation يستخدم خيار ضغط TIFF المرتبط بكل تمثيل صورة ، والذي قد لا يكون nstiffcompressionnone. وبالتالي ، يجب أن تكون واضحًا في الرغبة في عدم الضغط على Tiffdata.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top