2016-01-31 24 views
6

Mac Appの画像のサイズを変更しています。私ができることをしたいのは、PDFImageRepを持つサイズ変更されるイメージをユーザーがインポートし、自分の選択した解像度で新しいPDFファイルを保存する場合です。サイズ変更PDF NSImage OSX

は、これまでのところ私はのように、新しいサイズで画像を描画しようとしました:

- (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; 
} 

が、これは、PDF画像の担当者を失い、それゆえ私の省のコードは失敗します。

どうすればいいですか?私が使用しようとしている画像はベクタベースでなければならないので、新しい解像度を指定するためにPDFのプロパティを更新するだけの方法はありますか?

答えて

1

次の方法を試してください。これにより、ファイルに保存できる新しいPDFデータオブジェクトが作成されます。その後

// 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); 

正しい答えを供給するための

0

おかげヤコブを提出するpdfDataを保存します。もし誰かが興味があれば、私が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); 
    } 
}