2016-01-31 18 views
6

Sto aggiornando un'immagine che ridimensiona l'app Mac. Quello che voglio essere in grado di fare è se l'utente importa un'immagine da ridimensionare che ha un PDFImageRep, salva un nuovo file PDF con una risoluzione a mia scelta.Ridimensiona PDF NSImage OSX

Finora ho cercato di disegnare l'immagine in una nuova dimensione, come in:

- (NSImage*)imageAtSize:(NSSize)newSize 
{ 
    NSImage *resizedImage = [[NSImage alloc] initWithSize:newSize]; 

    [resizedImage lockFocus]; 
    [self drawInRect:NSMakeRect(0, 0, newSize.width, newSize.height) 
      fromRect:NSMakeRect(0, 0, self.size.width, self.size.height) 
      operation:NSCompositeSourceOver fraction:1.0]; 
    [resizedImage unlockFocus]; 

    return resizedImage; 
} 

ma questo perde l'immagine rappresentante PDF, e rende quindi il mio codice risparmio sicuro.

- (void)saveAsPDFWithOutputDirectory:(NSURL *)outputDirectory size:(NSSize)newSize 
{ 
    NSPDFImageRep *pdfRep = [self PDFImageRep]; 
    [pdfRep setSize:newSize]; 

    NSError *error = nil; 
    [pdfRep.PDFRepresentation writeToURL:outputDirectory options:NSDataWritingAtomic error:&error]; 
    if (error) 
    { 
     CLS_LOG(@"Error saving image: %@", error); 
    } 
} 

Quindi, come faccio? Le immagini che userò dovrebbero essere basate su vettori, quindi c'è un modo per aggiornare una proprietà sul PDF per specificare una nuova risoluzione?

risposta

1

Provare il seguente approccio. Questo crea un nuovo oggetto dati PDF che è possibile salvare in un file.

// pdfData is a CFMutableDataRef 
// auxInfo is a CFDictionary 
// bounds is your new size in points! 
CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(pdfData); 
CGContextRef context = CGPDFContextCreate(consumer, &bounds, auxInfo); 
CGPDFContextBeginPage(context, NULL); 
NSGraphicsContext *gc = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]; 
[NSGraphicsContext saveGraphicsState]; 
[NSGraphicsContext setCurrentContext:gc]; 

// Your drawing code here 
// You probably want to draw your NSImage here in the bounds 

[NSGraphicsContext restoreGraphicsState]; 
CGPDFContextEndPage(context); 
CGPDFContextClose(context); 
CGContextRelease(context); 
CGDataConsumerRelease(consumer); 

quindi salvare pdfData su file

0

Grazie Jacob per fornire la risposta corretta. Se qualcuno è interessato alla categoria completa che ho scritto con la tecnica di Jacob è:

@implementation NSImage (MKBSaveAsPDF) 

- (BOOL)isPDF 
{ 
    return ([self PDFImageRep] != nil); 
} 

- (NSPDFImageRep * _Nullable)PDFImageRep 
{ 
    for (NSImageRep *imageRep in self.representations) 
    { 
     if ([imageRep isKindOfClass:[NSPDFImageRep class]]){ 
      return (NSPDFImageRep*)imageRep; 
     } 
    } 

    return nil; 
} 

- (void)saveAsPDFWithOutputDirectory:(NSURL *)outputDirectory size:(NSSize)newSize 
{ 
    NSPDFImageRep *pdfRep = self.PDFImageRep; 

    NSMutableData *mutablePDFRef = [[NSMutableData alloc]initWithData:pdfRep.PDFRepresentation]; 

    CFMutableDataRef pdfDataRef = (__bridge CFMutableDataRef)(mutablePDFRef); 
    CGRect bounds = CGRectMake(0, 0, newSize.width, newSize.height); 

    CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(pdfDataRef); 
    CGContextRef context = CGPDFContextCreate(consumer, &bounds, nil); 
    CGPDFContextBeginPage(context, NULL); 
    NSGraphicsContext *gc = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]; 
    [NSGraphicsContext saveGraphicsState]; 
    [NSGraphicsContext setCurrentContext:gc]; 

    [self drawInRect:NSMakeRect(0, 0, newSize.width, newSize.height)]; 

    [NSGraphicsContext restoreGraphicsState]; 
    CGPDFContextEndPage(context); 
    CGPDFContextClose(context); 
    CGContextRelease(context); 
    CGDataConsumerRelease(consumer); 


    NSError *error = nil; 

    NSData *pdfData = (__bridge NSData *)(pdfDataRef); 
    [pdfData writeToURL:outputDirectory options:NSDataWritingAtomic error:&error]; 
    if (error) 
    { 
     NSLog(@"Error saving image: %@", error); 
    } 
}